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"
31 #include "dbus-connection-internal.h"
35 * @defgroup DBusBus Message bus APIs
37 * @brief Functions for communicating with the message bus
39 * @todo right now the default address of the system bus is hardcoded,
40 * so if you change it in the global config file suddenly you have to
41 * set DBUS_SYSTEM_BUS_ADDRESS env variable. Might be nice if the
42 * client lib somehow read the config file, or if the bus on startup
43 * somehow wrote out its address to a well-known spot, but might also
48 * @defgroup DBusBusInternals Message bus APIs internals
49 * @ingroup DBusInternals
50 * @brief Internals of functions for communicating with the message bus
56 * Block of message-bus-related data we attach to each
57 * #DBusConnection used with these convenience functions.
62 DBusConnection *connection; /**< Connection we're associated with */
63 char *unique_name; /**< Unique name of this connection */
65 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
68 /** The slot we have reserved to store BusData.
70 static dbus_int32_t bus_data_slot = -1;
72 /** Number of bus types */
75 static DBusConnection *bus_connections[N_BUS_TYPES];
76 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
78 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
80 static dbus_bool_t initialized = FALSE;
83 * Lock for globals in this file
85 _DBUS_DEFINE_GLOBAL_LOCK (bus);
88 addresses_shutdown_func (void *data)
93 while (i < N_BUS_TYPES)
95 if (bus_connections[i] != NULL)
96 _dbus_warn ("dbus_shutdown() called but connections were still live!");
98 dbus_free (bus_connection_addresses[i]);
99 bus_connection_addresses[i] = NULL;
103 activation_bus_type = DBUS_BUS_STARTER;
107 get_from_env (char **connection_p,
112 _dbus_assert (*connection_p == NULL);
114 s = _dbus_getenv (env_var);
115 if (s == NULL || *s == '\0')
116 return TRUE; /* successfully didn't use the env var */
119 *connection_p = _dbus_strdup (s);
120 return *connection_p != NULL;
125 init_connections_unlocked (void)
133 while (i < N_BUS_TYPES)
135 bus_connections[i] = NULL;
139 /* Don't init these twice, we may run this code twice if
140 * init_connections_unlocked() fails midway through.
141 * In practice, each block below should contain only one
142 * "return FALSE" or running through twice may not
146 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
148 _dbus_verbose ("Filling in system bus address...\n");
150 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
151 "DBUS_SYSTEM_BUS_ADDRESS"))
156 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
158 /* Use default system bus address if none set in environment */
159 bus_connection_addresses[DBUS_BUS_SYSTEM] =
160 _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
161 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
164 _dbus_verbose (" used default system bus \"%s\"\n",
165 bus_connection_addresses[DBUS_BUS_SYSTEM]);
168 _dbus_verbose (" used env var system bus \"%s\"\n",
169 bus_connection_addresses[DBUS_BUS_SYSTEM]);
171 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
173 _dbus_verbose ("Filling in session bus address...\n");
175 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
176 "DBUS_SESSION_BUS_ADDRESS"))
179 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
180 bus_connection_addresses[DBUS_BUS_SESSION] =
181 _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
182 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
185 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
186 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
189 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
191 _dbus_verbose ("Filling in activation bus address...\n");
193 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
194 "DBUS_STARTER_ADDRESS"))
197 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
198 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
202 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
204 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
208 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
210 if (strcmp (s, "system") == 0)
211 activation_bus_type = DBUS_BUS_SYSTEM;
212 else if (strcmp (s, "session") == 0)
213 activation_bus_type = DBUS_BUS_SESSION;
218 /* Default to the session bus instead if available */
219 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
221 bus_connection_addresses[DBUS_BUS_STARTER] =
222 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
223 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
228 /* If we return FALSE we have to be sure that restarting
229 * the above code will work right
232 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
235 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
238 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
249 bus_data_free (void *data)
253 if (bd->is_well_known)
257 /* We may be stored in more than one slot */
259 while (i < N_BUS_TYPES)
261 if (bus_connections[i] == bd->connection)
262 bus_connections[i] = NULL;
269 dbus_free (bd->unique_name);
272 dbus_connection_free_data_slot (&bus_data_slot);
276 ensure_bus_data (DBusConnection *connection)
280 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
283 bd = dbus_connection_get_data (connection, bus_data_slot);
286 bd = dbus_new0 (BusData, 1);
289 dbus_connection_free_data_slot (&bus_data_slot);
293 bd->connection = connection;
295 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
299 dbus_connection_free_data_slot (&bus_data_slot);
303 /* Data slot refcount now held by the BusData */
307 dbus_connection_free_data_slot (&bus_data_slot);
313 /* internal function that checks to see if this
314 is a shared connection owned by the bus and if it is unref it */
316 _dbus_bus_check_connection_and_unref_unlocked (DBusConnection *connection)
320 if (bus_connections[DBUS_BUS_SYSTEM] == connection)
322 bus_connections[DBUS_BUS_SYSTEM] = NULL;
323 _dbus_connection_unref_unlocked (connection);
325 else if (bus_connections[DBUS_BUS_SESSION] == connection)
327 bus_connections[DBUS_BUS_SESSION] = NULL;
328 _dbus_connection_unref_unlocked (connection);
330 else if (bus_connections[DBUS_BUS_STARTER] == connection)
332 bus_connections[DBUS_BUS_STARTER] = NULL;
333 _dbus_connection_unref_unlocked (connection);
339 static DBusConnection *
340 internal_bus_get (DBusBusType type,
341 DBusError *error, dbus_bool_t private)
344 DBusConnection *connection;
346 DBusBusType address_type;
348 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
349 _dbus_return_val_if_error_is_set (error, NULL);
353 if (!init_connections_unlocked ())
356 _DBUS_SET_OOM (error);
360 /* We want to use the activation address even if the
361 * activating bus is the session or system bus,
366 /* Use the real type of the activation bus for getting its
367 * connection, but only if the real type's address is available. (If
368 * the activating bus isn't a well-known bus then
369 * activation_bus_type == DBUS_BUS_STARTER)
371 if (type == DBUS_BUS_STARTER &&
372 bus_connection_addresses[activation_bus_type] != NULL)
373 type = activation_bus_type;
375 if (!private && bus_connections[type] != NULL)
377 connection = bus_connections[type];
378 dbus_connection_ref (connection);
384 address = bus_connection_addresses[address_type];
387 dbus_set_error (error, DBUS_ERROR_FAILED,
388 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
394 connection = dbus_connection_open_private(address, error);
396 connection = dbus_connection_open (address, error);
400 _DBUS_ASSERT_ERROR_IS_SET (error);
405 /* By default we're bound to the lifecycle of
408 dbus_connection_set_exit_on_disconnect (connection,
411 if (!dbus_bus_register (connection, error))
413 _DBUS_ASSERT_ERROR_IS_SET (error);
414 _dbus_connection_close_internal (connection);
415 dbus_connection_unref (connection);
423 /* get a hard ref to the connection */
424 bus_connections[type] = connection;
425 dbus_connection_ref (bus_connections[type]);
428 bd = ensure_bus_data (connection);
429 _dbus_assert (bd != NULL);
431 bd->is_well_known = TRUE;
438 /** @} */ /* end of implementation details docs */
441 * @addtogroup DBusBus
446 * Connects to a bus daemon and registers the client with it. If a
447 * connection to the bus already exists, then that connection is
448 * returned. Caller owns a reference to the bus.
450 * @param type bus type
451 * @param error address where an error can be returned.
452 * @returns a DBusConnection with new ref
455 dbus_bus_get (DBusBusType type,
457 return internal_bus_get(type, error, FALSE);
461 * Connects to a bus daemon and registers the client with it. Unlike
462 * dbus_bus_get(), always creates a new connection. This connection
463 * will not be saved or recycled by libdbus. Caller owns a reference
466 * @param type bus type
467 * @param error address where an error can be returned.
468 * @returns a DBusConnection with new ref
471 dbus_bus_get_private (DBusBusType type,
473 return internal_bus_get(type, error, TRUE);
477 * Registers a connection with the bus. This must be the first
478 * thing an application does when connecting to the message bus.
479 * If registration succeeds, the unique name will be set,
480 * and can be obtained using dbus_bus_get_unique_name().
482 * @param connection the connection
483 * @param error place to store errors
484 * @returns #TRUE on success
487 dbus_bus_register (DBusConnection *connection,
490 DBusMessage *message, *reply;
495 _dbus_return_val_if_fail (connection != NULL, FALSE);
496 _dbus_return_val_if_error_is_set (error, FALSE);
500 bd = ensure_bus_data (connection);
503 _DBUS_SET_OOM (error);
507 if (bd->unique_name != NULL)
509 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
510 /* This isn't an error, it's a programming bug. We'll be nice
511 * and not _dbus_assert_not_reached()
516 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
523 _DBUS_SET_OOM (error);
527 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
529 dbus_message_unref (message);
533 else if (dbus_set_error_from_message (error, reply))
535 else if (!dbus_message_get_args (reply, error,
536 DBUS_TYPE_STRING, &name,
540 bd->unique_name = _dbus_strdup (name);
541 if (bd->unique_name == NULL)
543 _DBUS_SET_OOM (error);
551 dbus_message_unref (reply);
554 _DBUS_ASSERT_ERROR_IS_SET (error);
561 * Sets the unique name of the connection. Can only be used if you
562 * registered with the bus manually (i.e. if you did not call
563 * dbus_bus_register()). Can only be called once per connection.
565 * @param connection the connection
566 * @param unique_name the unique name
567 * @returns #FALSE if not enough memory
570 dbus_bus_set_unique_name (DBusConnection *connection,
571 const char *unique_name)
575 _dbus_return_val_if_fail (connection != NULL, FALSE);
576 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
578 bd = ensure_bus_data (connection);
582 _dbus_assert (bd->unique_name == NULL);
584 bd->unique_name = _dbus_strdup (unique_name);
585 return bd->unique_name != NULL;
589 * Gets the unique name of the connection. Only possible after the
590 * connection has been registered with the message bus.
592 * The name remains valid for the duration of the connection and
593 * should not be freed by the caller.
595 * @param connection the connection
596 * @returns the unique name or NULL on error
599 dbus_bus_get_unique_name (DBusConnection *connection)
603 _dbus_return_val_if_fail (connection != NULL, NULL);
605 bd = ensure_bus_data (connection);
609 return bd->unique_name;
613 * Asks the bus to return the uid of the named
616 * Not going to work on Windows, the bus should return
619 * @param connection the connection
620 * @param name a name owned by the connection
621 * @param error location to store the error
622 * @returns a result code, -1 if error is set
625 dbus_bus_get_unix_user (DBusConnection *connection,
629 DBusMessage *message, *reply;
632 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
633 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
634 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
635 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
637 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
640 "GetConnectionUnixUser");
644 _DBUS_SET_OOM (error);
645 return DBUS_UID_UNSET;
648 if (!dbus_message_append_args (message,
649 DBUS_TYPE_STRING, &name,
652 dbus_message_unref (message);
653 _DBUS_SET_OOM (error);
654 return DBUS_UID_UNSET;
657 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
660 dbus_message_unref (message);
664 _DBUS_ASSERT_ERROR_IS_SET (error);
665 return DBUS_UID_UNSET;
668 if (dbus_set_error_from_message (error, reply))
670 _DBUS_ASSERT_ERROR_IS_SET (error);
671 dbus_message_unref (reply);
672 return DBUS_UID_UNSET;
675 if (!dbus_message_get_args (reply, error,
676 DBUS_TYPE_UINT32, &uid,
679 _DBUS_ASSERT_ERROR_IS_SET (error);
680 dbus_message_unref (reply);
681 return DBUS_UID_UNSET;
684 dbus_message_unref (reply);
686 return (unsigned long) uid;
691 * Asks the bus to assign the given name to this connection by invoking
692 * the RequestName method on the bus. This method is fully documented
693 * in the D-Bus specification. For quick reference, the flags and
694 * result codes are discussed here, but the specification is the
695 * canonical version of this information.
697 * The #DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag indicates that the caller
698 * will allow other services to take over the name from the current owner.
700 * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
701 * would like to take over the name from the current owner.
702 * If the current name owner did not use #DBUS_NAME_FLAG_ALLOW_REPLACEMENT
703 * then this flag indicates that the caller would like to be placed
704 * in the queue to own the name when the current owner lets go.
706 * If no flags are given, an application will receive the requested
707 * name only if the name is currently unowned; it will NOT give
708 * up the name if another application asks to take it over using
709 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
711 * This function returns a result code. The possible result codes
714 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
715 * existing owner, and the caller is now the primary owner; or that
716 * the name had an owner, and the caller specified
717 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
718 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
720 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
721 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
722 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
723 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
724 * in a queue to own the name after the current owner gives it up.
726 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
727 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
728 * and either the current owner has NOT specified
729 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
730 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
732 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
733 * requests a name it already owns.
735 * When a service represents an application, say "text editor," then
736 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
737 * the last editor started to be the user's editor vs. the first one
738 * started. Then any editor that can be the user's editor should
739 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
740 * (last-started-wins) or be queued up (first-started-wins) according
741 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
743 * @param connection the connection
744 * @param name the name to request
746 * @param error location to store the error
747 * @returns a result code, -1 if error is set
750 dbus_bus_request_name (DBusConnection *connection,
755 DBusMessage *message, *reply;
756 dbus_uint32_t result;
758 _dbus_return_val_if_fail (connection != NULL, 0);
759 _dbus_return_val_if_fail (name != NULL, 0);
760 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
761 _dbus_return_val_if_error_is_set (error, 0);
763 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
770 _DBUS_SET_OOM (error);
774 if (!dbus_message_append_args (message,
775 DBUS_TYPE_STRING, &name,
776 DBUS_TYPE_UINT32, &flags,
779 dbus_message_unref (message);
780 _DBUS_SET_OOM (error);
784 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
787 dbus_message_unref (message);
791 _DBUS_ASSERT_ERROR_IS_SET (error);
795 if (dbus_set_error_from_message (error, reply))
797 _DBUS_ASSERT_ERROR_IS_SET (error);
798 dbus_message_unref (reply);
802 if (!dbus_message_get_args (reply, error,
803 DBUS_TYPE_UINT32, &result,
806 _DBUS_ASSERT_ERROR_IS_SET (error);
807 dbus_message_unref (reply);
811 dbus_message_unref (reply);
818 * Asks the bus to unassign the given name to this connection by invoking
819 * the ReleaseName method on the bus. This method is fully documented
820 * in the D-Bus specification.
822 * @param connection the connection
823 * @param name the name to remove
824 * @param error location to store the error
825 * @returns a result code, -1 if error is set
828 dbus_bus_release_name (DBusConnection *connection,
832 DBusMessage *message, *reply;
833 dbus_uint32_t result;
835 _dbus_return_val_if_fail (connection != NULL, 0);
836 _dbus_return_val_if_fail (name != NULL, 0);
837 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
838 _dbus_return_val_if_error_is_set (error, 0);
840 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
847 _DBUS_SET_OOM (error);
851 if (!dbus_message_append_args (message,
852 DBUS_TYPE_STRING, &name,
855 dbus_message_unref (message);
856 _DBUS_SET_OOM (error);
860 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
863 dbus_message_unref (message);
867 _DBUS_ASSERT_ERROR_IS_SET (error);
871 if (dbus_set_error_from_message (error, reply))
873 _DBUS_ASSERT_ERROR_IS_SET (error);
874 dbus_message_unref (reply);
878 if (!dbus_message_get_args (reply, error,
879 DBUS_TYPE_UINT32, &result,
882 _DBUS_ASSERT_ERROR_IS_SET (error);
883 dbus_message_unref (reply);
887 dbus_message_unref (reply);
893 * Checks whether a certain name has an owner.
895 * @param connection the connection
896 * @param name the name
897 * @param error location to store any errors
898 * @returns #TRUE if the name exists, #FALSE if not or on error
901 dbus_bus_name_has_owner (DBusConnection *connection,
905 DBusMessage *message, *reply;
908 _dbus_return_val_if_fail (connection != NULL, FALSE);
909 _dbus_return_val_if_fail (name != NULL, FALSE);
910 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
911 _dbus_return_val_if_error_is_set (error, FALSE);
913 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
919 _DBUS_SET_OOM (error);
923 if (!dbus_message_append_args (message,
924 DBUS_TYPE_STRING, &name,
927 dbus_message_unref (message);
928 _DBUS_SET_OOM (error);
932 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
933 dbus_message_unref (message);
937 _DBUS_ASSERT_ERROR_IS_SET (error);
941 if (!dbus_message_get_args (reply, error,
942 DBUS_TYPE_BOOLEAN, &exists,
945 _DBUS_ASSERT_ERROR_IS_SET (error);
946 dbus_message_unref (reply);
950 dbus_message_unref (reply);
955 * Starts a service that will request ownership of the given name.
956 * The returned result will be one of be one of
957 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
958 * successful. Pass #NULL if you don't care about the result.
960 * The flags parameter is for future expansion, currently you should
963 * @param connection the connection
964 * @param name the name we want the new service to request
965 * @param flags the flags (should always be 0 for now)
966 * @param result a place to store the result or #NULL
967 * @param error location to store any errors
968 * @returns #TRUE if the activation succeeded, #FALSE if not
971 dbus_bus_start_service_by_name (DBusConnection *connection,
974 dbus_uint32_t *result,
980 _dbus_return_val_if_fail (connection != NULL, FALSE);
981 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
983 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
986 "StartServiceByName");
988 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
989 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
991 dbus_message_unref (msg);
992 _DBUS_SET_OOM (error);
996 reply = dbus_connection_send_with_reply_and_block (connection, msg,
998 dbus_message_unref (msg);
1002 _DBUS_ASSERT_ERROR_IS_SET (error);
1006 if (dbus_set_error_from_message (error, reply))
1008 _DBUS_ASSERT_ERROR_IS_SET (error);
1009 dbus_message_unref (reply);
1013 if (result != NULL &&
1014 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1015 result, DBUS_TYPE_INVALID))
1017 _DBUS_ASSERT_ERROR_IS_SET (error);
1018 dbus_message_unref (reply);
1022 dbus_message_unref (reply);
1027 send_no_return_values (DBusConnection *connection,
1033 /* Block to check success codepath */
1036 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1040 _DBUS_ASSERT_ERROR_IS_SET (error);
1042 dbus_message_unref (reply);
1046 /* Silently-fail nonblocking codepath */
1047 dbus_message_set_no_reply (msg, TRUE);
1048 dbus_connection_send (connection, msg, NULL);
1053 * Adds a match rule to match messages going through the message bus.
1054 * The "rule" argument is the string form of a match rule.
1056 * If you pass #NULL for the error, this function will not
1057 * block; the match thus won't be added until you flush the
1058 * connection, and if there's an error adding the match
1059 * (only possible error is lack of resources in the bus),
1060 * you won't find out about it.
1062 * If you pass non-#NULL for the error this function will
1063 * block until it gets a reply.
1065 * Normal API conventions would have the function return
1066 * a boolean value indicating whether the error was set,
1067 * but that would require blocking always to determine
1070 * The AddMatch method is fully documented in the D-Bus
1071 * specification. For quick reference, the format of the
1072 * match rules is discussed here, but the specification
1073 * is the canonical version of this information.
1075 * Rules are specified as a string of comma separated
1076 * key/value pairs. An example is
1077 * "type='signal',sender='org.freedesktop.DBus',
1078 * interface='org.freedesktop.DBus',member='Foo',
1079 * path='/bar/foo',destination=':452345.34'"
1081 * Possible keys you can match on are type, sender,
1082 * interface, member, path, destination and the special
1083 * arg keys. Excluding a key from the rule indicates
1084 * a wildcard match. For instance excluding the
1085 * the member from a match rule but adding a sender would
1086 * let all messages from that sender through.
1088 * Matches are inclusive not exclusive so as long as one
1089 * rule matches the message will get through. It is important
1090 * to note this because every time a message is received the
1091 * application will be paged into memory to process it. This
1092 * can cause performance problems such as draining batteries
1093 * on embedded platforms.
1095 * The special arg keys are used for further restricting the
1096 * match based on the parameters sent by the signal or method.
1097 * For instance arg1='foo' will check the first argument,
1098 * arg2='bar' the second and so on. For performance reasons
1099 * there is a set limit on the highest number parameter that
1100 * can be checked which is set in dbus-protocol.h
1102 * @param connection connection to the message bus
1103 * @param rule textual form of match rule
1104 * @param error location to store any errors
1107 dbus_bus_add_match (DBusConnection *connection,
1113 _dbus_return_if_fail (rule != NULL);
1115 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1117 DBUS_INTERFACE_DBUS,
1122 _DBUS_SET_OOM (error);
1126 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1129 dbus_message_unref (msg);
1130 _DBUS_SET_OOM (error);
1134 send_no_return_values (connection, msg, error);
1136 dbus_message_unref (msg);
1140 * Removes a previously-added match rule "by value" (the most
1141 * recently-added identical rule gets removed). The "rule" argument
1142 * is the string form of a match rule.
1144 * If you pass #NULL for the error, this function will not
1145 * block; otherwise it will. See detailed explanation in
1146 * docs for dbus_bus_add_match().
1148 * @param connection connection to the message bus
1149 * @param rule textual form of match rule
1150 * @param error location to store any errors
1153 dbus_bus_remove_match (DBusConnection *connection,
1159 _dbus_return_if_fail (rule != NULL);
1161 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1163 DBUS_INTERFACE_DBUS,
1166 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1169 dbus_message_unref (msg);
1170 _DBUS_SET_OOM (error);
1174 send_no_return_values (connection, msg, error);
1176 dbus_message_unref (msg);