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);
162 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
165 _dbus_verbose (" used default system bus \"%s\"\n",
166 bus_connection_addresses[DBUS_BUS_SYSTEM]);
169 _dbus_verbose (" used env var system bus \"%s\"\n",
170 bus_connection_addresses[DBUS_BUS_SYSTEM]);
172 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
174 _dbus_verbose ("Filling in session bus address...\n");
176 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
177 "DBUS_SESSION_BUS_ADDRESS"))
180 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
181 bus_connection_addresses[DBUS_BUS_SESSION] =
182 _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
184 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
187 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
188 bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
191 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
193 _dbus_verbose ("Filling in activation bus address...\n");
195 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
196 "DBUS_STARTER_ADDRESS"))
199 _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
200 bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
204 if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
206 s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
210 _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
212 if (strcmp (s, "system") == 0)
213 activation_bus_type = DBUS_BUS_SYSTEM;
214 else if (strcmp (s, "session") == 0)
215 activation_bus_type = DBUS_BUS_SESSION;
220 /* Default to the session bus instead if available */
221 if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
223 bus_connection_addresses[DBUS_BUS_STARTER] =
224 _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
225 if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
230 /* If we return FALSE we have to be sure that restarting
231 * the above code will work right
234 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
237 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
240 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
251 bus_data_free (void *data)
255 if (bd->is_well_known)
259 /* We may be stored in more than one slot */
260 /* This should now be impossible - these slots are supposed to
261 * be cleared on disconnect, so should not need to be cleared on
265 while (i < N_BUS_TYPES)
267 if (bus_connections[i] == bd->connection)
268 bus_connections[i] = NULL;
275 dbus_free (bd->unique_name);
278 dbus_connection_free_data_slot (&bus_data_slot);
282 ensure_bus_data (DBusConnection *connection)
286 if (!dbus_connection_allocate_data_slot (&bus_data_slot))
289 bd = dbus_connection_get_data (connection, bus_data_slot);
292 bd = dbus_new0 (BusData, 1);
295 dbus_connection_free_data_slot (&bus_data_slot);
299 bd->connection = connection;
301 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
305 dbus_connection_free_data_slot (&bus_data_slot);
309 /* Data slot refcount now held by the BusData */
313 dbus_connection_free_data_slot (&bus_data_slot);
320 * Internal function that checks to see if this
321 * is a shared connection owned by the bus and if it is unref it.
323 * @param connection a connection that has been disconnected.
326 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
332 /* We are expecting to have the connection saved in only one of these
333 * slots, but someone could in a pathological case set system and session
334 * bus to the same bus or something. Or set one of them to the starter
335 * bus without setting the starter bus type in the env variable.
336 * So we don't break the loop as soon as we find a match.
338 for (i = 0; i < N_BUS_TYPES; ++i)
340 if (bus_connections[i] == connection)
342 bus_connections[i] = NULL;
349 static DBusConnection *
350 internal_bus_get (DBusBusType type,
355 DBusConnection *connection;
357 DBusBusType address_type;
359 _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
360 _dbus_return_val_if_error_is_set (error, NULL);
364 if (!init_connections_unlocked ())
367 _DBUS_SET_OOM (error);
371 /* We want to use the activation address even if the
372 * activating bus is the session or system bus,
377 /* Use the real type of the activation bus for getting its
378 * connection, but only if the real type's address is available. (If
379 * the activating bus isn't a well-known bus then
380 * activation_bus_type == DBUS_BUS_STARTER)
382 if (type == DBUS_BUS_STARTER &&
383 bus_connection_addresses[activation_bus_type] != NULL)
384 type = activation_bus_type;
386 if (!private && bus_connections[type] != NULL)
388 connection = bus_connections[type];
389 dbus_connection_ref (connection);
395 address = bus_connection_addresses[address_type];
398 dbus_set_error (error, DBUS_ERROR_FAILED,
399 "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
405 connection = dbus_connection_open_private (address, error);
407 connection = dbus_connection_open (address, error);
411 _DBUS_ASSERT_ERROR_IS_SET (error);
416 /* By default we're bound to the lifecycle of
419 dbus_connection_set_exit_on_disconnect (connection,
422 if (!dbus_bus_register (connection, error))
424 _DBUS_ASSERT_ERROR_IS_SET (error);
425 _dbus_connection_close_possibly_shared (connection);
426 dbus_connection_unref (connection);
434 /* store a weak ref to the connection (dbus-connection.c is
435 * supposed to have a strong ref that it drops on disconnect,
436 * since this is a shared connection)
438 bus_connections[type] = connection;
441 bd = ensure_bus_data (connection);
442 _dbus_assert (bd != NULL);
444 bd->is_well_known = TRUE;
448 /* Return a reference to the caller */
453 /** @} */ /* end of implementation details docs */
456 * @addtogroup DBusBus
461 * Connects to a bus daemon and registers the client with it. If a
462 * connection to the bus already exists, then that connection is
463 * returned. The caller of this function owns a reference to the bus.
465 * The caller may NOT call dbus_connection_close() on this connection;
466 * see dbus_connection_open() and dbus_connection_close() for details
469 * If this function obtains a new connection object never before
470 * returned from dbus_bus_get(), it will call
471 * dbus_connection_set_exit_on_disconnect(), so the application
472 * will exit if the connection closes. You can undo this
473 * by calling dbus_connection_set_exit_on_disconnect() yourself
474 * after you get the connection.
476 * @param type bus type
477 * @param error address where an error can be returned.
478 * @returns a #DBusConnection with new ref
481 dbus_bus_get (DBusBusType type,
484 return internal_bus_get (type, FALSE, error);
488 * Connects to a bus daemon and registers the client with it as with dbus_bus_register().
489 * Unlike dbus_bus_get(), always creates a new connection. This connection
490 * will not be saved or recycled by libdbus. Caller owns a reference
491 * to the bus and must either close it or know it to be closed
492 * prior to releasing this reference.
494 * See dbus_connection_open_private() for more details on when to
495 * close and unref this connection.
497 * This function calls
498 * dbus_connection_set_exit_on_disconnect() on the new connection, so the application
499 * will exit if the connection closes. You can undo this
500 * by calling dbus_connection_set_exit_on_disconnect() yourself
501 * after you get the connection.
503 * @param type bus type
504 * @param error address where an error can be returned.
505 * @returns a DBusConnection with new ref
508 dbus_bus_get_private (DBusBusType type,
511 return internal_bus_get (type, TRUE, error);
515 * Registers a connection with the bus. This must be the first
516 * thing an application does when connecting to the message bus.
517 * If registration succeeds, the unique name will be set,
518 * and can be obtained using dbus_bus_get_unique_name().
520 * If you use dbus_bus_get() or dbus_bus_get_private() this
521 * function will be called for you.
523 * @param connection the connection
524 * @param error place to store errors
525 * @returns #TRUE on success
528 dbus_bus_register (DBusConnection *connection,
531 DBusMessage *message, *reply;
536 _dbus_return_val_if_fail (connection != NULL, FALSE);
537 _dbus_return_val_if_error_is_set (error, FALSE);
541 bd = ensure_bus_data (connection);
544 _DBUS_SET_OOM (error);
548 if (bd->unique_name != NULL)
550 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
551 /* This isn't an error, it's a programming bug. We'll be nice
552 * and not _dbus_assert_not_reached()
557 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
564 _DBUS_SET_OOM (error);
568 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
570 dbus_message_unref (message);
574 else if (dbus_set_error_from_message (error, reply))
576 else if (!dbus_message_get_args (reply, error,
577 DBUS_TYPE_STRING, &name,
581 bd->unique_name = _dbus_strdup (name);
582 if (bd->unique_name == NULL)
584 _DBUS_SET_OOM (error);
592 dbus_message_unref (reply);
595 _DBUS_ASSERT_ERROR_IS_SET (error);
602 * Sets the unique name of the connection. Can only be used if you
603 * registered with the bus manually (i.e. if you did not call
604 * dbus_bus_register()). Can only be called once per connection.
606 * @param connection the connection
607 * @param unique_name the unique name
608 * @returns #FALSE if not enough memory
611 dbus_bus_set_unique_name (DBusConnection *connection,
612 const char *unique_name)
616 _dbus_return_val_if_fail (connection != NULL, FALSE);
617 _dbus_return_val_if_fail (unique_name != NULL, FALSE);
619 bd = ensure_bus_data (connection);
623 _dbus_assert (bd->unique_name == NULL);
625 bd->unique_name = _dbus_strdup (unique_name);
626 return bd->unique_name != NULL;
630 * Gets the unique name of the connection. Only possible after the
631 * connection has been registered with the message bus.
633 * The name remains valid for the duration of the connection and
634 * should not be freed by the caller.
636 * @param connection the connection
637 * @returns the unique name or NULL on error
640 dbus_bus_get_unique_name (DBusConnection *connection)
644 _dbus_return_val_if_fail (connection != NULL, NULL);
646 bd = ensure_bus_data (connection);
650 return bd->unique_name;
654 * Asks the bus to return the uid of the named
657 * Not going to work on Windows, the bus should return
660 * @param connection the connection
661 * @param name a name owned by the connection
662 * @param error location to store the error
663 * @returns a result code, -1 if error is set
666 dbus_bus_get_unix_user (DBusConnection *connection,
670 DBusMessage *message, *reply;
673 _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
674 _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
675 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
676 _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
678 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
681 "GetConnectionUnixUser");
685 _DBUS_SET_OOM (error);
686 return DBUS_UID_UNSET;
689 if (!dbus_message_append_args (message,
690 DBUS_TYPE_STRING, &name,
693 dbus_message_unref (message);
694 _DBUS_SET_OOM (error);
695 return DBUS_UID_UNSET;
698 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
701 dbus_message_unref (message);
705 _DBUS_ASSERT_ERROR_IS_SET (error);
706 return DBUS_UID_UNSET;
709 if (dbus_set_error_from_message (error, reply))
711 _DBUS_ASSERT_ERROR_IS_SET (error);
712 dbus_message_unref (reply);
713 return DBUS_UID_UNSET;
716 if (!dbus_message_get_args (reply, error,
717 DBUS_TYPE_UINT32, &uid,
720 _DBUS_ASSERT_ERROR_IS_SET (error);
721 dbus_message_unref (reply);
722 return DBUS_UID_UNSET;
725 dbus_message_unref (reply);
727 return (unsigned long) uid;
732 * Asks the bus to assign the given name to this connection by invoking
733 * the RequestName method on the bus. This method is fully documented
734 * in the D-Bus specification. For quick reference, the flags and
735 * result codes are discussed here, but the specification is the
736 * canonical version of this information.
738 * The #DBUS_NAME_FLAG_ALLOW_REPLACEMENT flag indicates that the caller
739 * will allow other services to take over the name from the current owner.
741 * The #DBUS_NAME_FLAG_REPLACE_EXISTING flag indicates that the caller
742 * would like to take over the name from the current owner.
743 * If the current name owner did not use #DBUS_NAME_FLAG_ALLOW_REPLACEMENT
744 * then this flag indicates that the caller would like to be placed
745 * in the queue to own the name when the current owner lets go.
747 * If no flags are given, an application will receive the requested
748 * name only if the name is currently unowned; it will NOT give
749 * up the name if another application asks to take it over using
750 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
752 * This function returns a result code. The possible result codes
755 * #DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER means that the name had no
756 * existing owner, and the caller is now the primary owner; or that
757 * the name had an owner, and the caller specified
758 * #DBUS_NAME_FLAG_REPLACE_EXISTING, and the current owner
759 * specified #DBUS_NAME_FLAG_ALLOW_REPLACEMENT.
761 * #DBUS_REQUEST_NAME_REPLY_IN_QUEUE happens only if the caller does NOT
762 * specify #DBUS_NAME_FLAG_DO_NOT_QUEUE and either the current owner
763 * did NOT specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT
764 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING. In this case the caller ends up
765 * in a queue to own the name after the current owner gives it up.
767 * #DBUS_REQUEST_NAME_REPLY_EXISTS happens if the name has an owner
768 * already and the caller specifies #DBUS_NAME_FLAG_DO_NOT_QUEUE
769 * and either the current owner has NOT specified
770 * #DBUS_NAME_FLAG_ALLOW_REPLACEMENT or the caller did NOT specify
771 * #DBUS_NAME_FLAG_REPLACE_EXISTING.
773 * #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER happens if an application
774 * requests a name it already owns.
776 * When a service represents an application, say "text editor," then
777 * it should specify #DBUS_NAME_FLAG_ALLOW_REPLACEMENT if it wants
778 * the last editor started to be the user's editor vs. the first one
779 * started. Then any editor that can be the user's editor should
780 * specify #DBUS_NAME_FLAG_REPLACE_EXISTING to either take over
781 * (last-started-wins) or be queued up (first-started-wins) according
782 * to whether #DBUS_NAME_FLAG_ALLOW_REPLACEMENT was given.
784 * @param connection the connection
785 * @param name the name to request
787 * @param error location to store the error
788 * @returns a result code, -1 if error is set
791 dbus_bus_request_name (DBusConnection *connection,
796 DBusMessage *message, *reply;
797 dbus_uint32_t result;
799 _dbus_return_val_if_fail (connection != NULL, 0);
800 _dbus_return_val_if_fail (name != NULL, 0);
801 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
802 _dbus_return_val_if_error_is_set (error, 0);
804 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
811 _DBUS_SET_OOM (error);
815 if (!dbus_message_append_args (message,
816 DBUS_TYPE_STRING, &name,
817 DBUS_TYPE_UINT32, &flags,
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);
859 * Asks the bus to unassign the given name to this connection by invoking
860 * the ReleaseName method on the bus. This method is fully documented
861 * in the D-Bus specification.
863 * @param connection the connection
864 * @param name the name to remove
865 * @param error location to store the error
866 * @returns a result code, -1 if error is set
869 dbus_bus_release_name (DBusConnection *connection,
873 DBusMessage *message, *reply;
874 dbus_uint32_t result;
876 _dbus_return_val_if_fail (connection != NULL, 0);
877 _dbus_return_val_if_fail (name != NULL, 0);
878 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
879 _dbus_return_val_if_error_is_set (error, 0);
881 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
888 _DBUS_SET_OOM (error);
892 if (!dbus_message_append_args (message,
893 DBUS_TYPE_STRING, &name,
896 dbus_message_unref (message);
897 _DBUS_SET_OOM (error);
901 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
904 dbus_message_unref (message);
908 _DBUS_ASSERT_ERROR_IS_SET (error);
912 if (dbus_set_error_from_message (error, reply))
914 _DBUS_ASSERT_ERROR_IS_SET (error);
915 dbus_message_unref (reply);
919 if (!dbus_message_get_args (reply, error,
920 DBUS_TYPE_UINT32, &result,
923 _DBUS_ASSERT_ERROR_IS_SET (error);
924 dbus_message_unref (reply);
928 dbus_message_unref (reply);
934 * Checks whether a certain name has an owner.
936 * @param connection the connection
937 * @param name the name
938 * @param error location to store any errors
939 * @returns #TRUE if the name exists, #FALSE if not or on error
942 dbus_bus_name_has_owner (DBusConnection *connection,
946 DBusMessage *message, *reply;
949 _dbus_return_val_if_fail (connection != NULL, FALSE);
950 _dbus_return_val_if_fail (name != NULL, FALSE);
951 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
952 _dbus_return_val_if_error_is_set (error, FALSE);
954 message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
960 _DBUS_SET_OOM (error);
964 if (!dbus_message_append_args (message,
965 DBUS_TYPE_STRING, &name,
968 dbus_message_unref (message);
969 _DBUS_SET_OOM (error);
973 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
974 dbus_message_unref (message);
978 _DBUS_ASSERT_ERROR_IS_SET (error);
982 if (!dbus_message_get_args (reply, error,
983 DBUS_TYPE_BOOLEAN, &exists,
986 _DBUS_ASSERT_ERROR_IS_SET (error);
987 dbus_message_unref (reply);
991 dbus_message_unref (reply);
996 * Starts a service that will request ownership of the given name.
997 * The returned result will be one of be one of
998 * #DBUS_START_REPLY_SUCCESS or #DBUS_START_REPLY_ALREADY_RUNNING if
999 * successful. Pass #NULL if you don't care about the result.
1001 * The flags parameter is for future expansion, currently you should
1004 * @param connection the connection
1005 * @param name the name we want the new service to request
1006 * @param flags the flags (should always be 0 for now)
1007 * @param result a place to store the result or #NULL
1008 * @param error location to store any errors
1009 * @returns #TRUE if the activation succeeded, #FALSE if not
1012 dbus_bus_start_service_by_name (DBusConnection *connection,
1014 dbus_uint32_t flags,
1015 dbus_uint32_t *result,
1021 _dbus_return_val_if_fail (connection != NULL, FALSE);
1022 _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1024 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1026 DBUS_INTERFACE_DBUS,
1027 "StartServiceByName");
1029 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1030 DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
1032 dbus_message_unref (msg);
1033 _DBUS_SET_OOM (error);
1037 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1039 dbus_message_unref (msg);
1043 _DBUS_ASSERT_ERROR_IS_SET (error);
1047 if (dbus_set_error_from_message (error, reply))
1049 _DBUS_ASSERT_ERROR_IS_SET (error);
1050 dbus_message_unref (reply);
1054 if (result != NULL &&
1055 !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1056 result, DBUS_TYPE_INVALID))
1058 _DBUS_ASSERT_ERROR_IS_SET (error);
1059 dbus_message_unref (reply);
1063 dbus_message_unref (reply);
1068 send_no_return_values (DBusConnection *connection,
1074 /* Block to check success codepath */
1077 reply = dbus_connection_send_with_reply_and_block (connection, msg,
1081 _DBUS_ASSERT_ERROR_IS_SET (error);
1083 dbus_message_unref (reply);
1087 /* Silently-fail nonblocking codepath */
1088 dbus_message_set_no_reply (msg, TRUE);
1089 dbus_connection_send (connection, msg, NULL);
1094 * Adds a match rule to match messages going through the message bus.
1095 * The "rule" argument is the string form of a match rule.
1097 * If you pass #NULL for the error, this function will not
1098 * block; the match thus won't be added until you flush the
1099 * connection, and if there's an error adding the match
1100 * (only possible error is lack of resources in the bus),
1101 * you won't find out about it.
1103 * If you pass non-#NULL for the error this function will
1104 * block until it gets a reply.
1106 * Normal API conventions would have the function return
1107 * a boolean value indicating whether the error was set,
1108 * but that would require blocking always to determine
1111 * The AddMatch method is fully documented in the D-Bus
1112 * specification. For quick reference, the format of the
1113 * match rules is discussed here, but the specification
1114 * is the canonical version of this information.
1116 * Rules are specified as a string of comma separated
1117 * key/value pairs. An example is
1118 * "type='signal',sender='org.freedesktop.DBus',
1119 * interface='org.freedesktop.DBus',member='Foo',
1120 * path='/bar/foo',destination=':452345.34'"
1122 * Possible keys you can match on are type, sender,
1123 * interface, member, path, destination and the special
1124 * arg keys. Excluding a key from the rule indicates
1125 * a wildcard match. For instance excluding the
1126 * the member from a match rule but adding a sender would
1127 * let all messages from that sender through.
1129 * Matches are inclusive not exclusive so as long as one
1130 * rule matches the message will get through. It is important
1131 * to note this because every time a message is received the
1132 * application will be paged into memory to process it. This
1133 * can cause performance problems such as draining batteries
1134 * on embedded platforms.
1136 * The special arg keys are used for further restricting the
1137 * match based on the parameters sent by the signal or method.
1138 * For instance arg1='foo' will check the first argument,
1139 * arg2='bar' the second and so on. For performance reasons
1140 * there is a set limit on the highest number parameter that
1141 * can be checked which is set in dbus-protocol.h
1143 * @param connection connection to the message bus
1144 * @param rule textual form of match rule
1145 * @param error location to store any errors
1148 dbus_bus_add_match (DBusConnection *connection,
1154 _dbus_return_if_fail (rule != NULL);
1156 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1158 DBUS_INTERFACE_DBUS,
1163 _DBUS_SET_OOM (error);
1167 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1170 dbus_message_unref (msg);
1171 _DBUS_SET_OOM (error);
1175 send_no_return_values (connection, msg, error);
1177 dbus_message_unref (msg);
1181 * Removes a previously-added match rule "by value" (the most
1182 * recently-added identical rule gets removed). The "rule" argument
1183 * is the string form of a match rule.
1185 * If you pass #NULL for the error, this function will not
1186 * block; otherwise it will. See detailed explanation in
1187 * docs for dbus_bus_add_match().
1189 * @param connection connection to the message bus
1190 * @param rule textual form of match rule
1191 * @param error location to store any errors
1194 dbus_bus_remove_match (DBusConnection *connection,
1200 _dbus_return_if_fail (rule != NULL);
1202 msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
1204 DBUS_INTERFACE_DBUS,
1207 if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1210 dbus_message_unref (msg);
1211 _DBUS_SET_OOM (error);
1215 send_no_return_values (connection, msg, error);
1217 dbus_message_unref (msg);