diff options
author | Holger Macht <holger@homac.de> | 2006-11-20 17:00:54 +0100 |
---|---|---|
committer | Holger Macht <holger@homac.de> | 2006-11-20 17:00:54 +0100 |
commit | 920e620f6b332d1df7a50961ca4ce87690e392c7 (patch) | |
tree | 1c568ab2423b958f7cef8910534b20ab6311eeef | |
parent | b876db8c69bd630c1fd407c5b72acc93fc49d175 (diff) |
add possbility to use private connection for system bus messages.
This way applications without a mainloop and seldom D-Bus calls
survive D-Bus restarts.
-rw-r--r-- | configure.in | 9 | ||||
-rw-r--r-- | liblazy/liblazy.h | 16 | ||||
-rw-r--r-- | liblazy/liblazy_dbus.c | 42 |
3 files changed, 60 insertions, 7 deletions
diff --git a/configure.in b/configure.in index 70d623b..dcb03e5 100644 --- a/configure.in +++ b/configure.in @@ -6,6 +6,7 @@ AC_PREREQ(2.59) AC_INIT(liblazy, 0.1.1, hmacht@suse.de) AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR([liblazy/liblazy_hal.c]) +AM_CONFIG_HEADER(config.h) # Checks for programs. AC_PROG_CC @@ -19,6 +20,13 @@ PKG_CHECK_MODULES(DBUS, dbus-1 >= 0.30) DBUS_VERSION="`pkg-config --modversion dbus-1`" +DBUS_SYSTEM_BUS_SOCKET="`pkg-config --variable=system_bus_default_address dbus-1`" +if test -z "$DBUS_SYSTEM_BUS_SOCKET"; then + # use default + DBUS_SYSTEM_BUS_SOCKET="unix:path=/var/run/dbus/system_bus_socket" +fi +AC_DEFINE_UNQUOTED(DBUS_SYSTEM_BUS_SOCKET, ["$DBUS_SYSTEM_BUS_SOCKET"], + [dbus system bus socket path]) # check for doxygen AC_CHECK_PROG(DOXYGEN, [doxygen], [doxygen], []) @@ -33,4 +41,3 @@ AC_CONFIG_FILES([Makefile docs/autodocs/Makefile]) AC_OUTPUT - diff --git a/liblazy/liblazy.h b/liblazy/liblazy.h index e3ce8a4..2a3bc0d 100644 --- a/liblazy/liblazy.h +++ b/liblazy/liblazy.h @@ -57,6 +57,22 @@ void liblazy_free_string(char *string); */ void liblazy_free_strlist(char **strlist); +/** @brief use a private connection for system bus messages + * + * Call this function with a boolean value to tell the library whether to + * use a private connection for system bus messages + * (liblazy_dbus_system*). Defaults to false. If a private connection is + * used, the libarary creates a new connection on _every_ call and + * immediately closes it. So this should be used very carefully. Only use + * if your application does very rare dbus calls and doesn't have a + * mainloop to listen if D-Bus died but still should be able to survive + * D-Bus restarts. + * + * @param use_privat 1 if the library should use a private connection, 0 + * otherwise + */ +void liblazy_dbus_system_use_private_connection(int use_private); + /** @brief send a method call to the system bus * * sends a method call to the system bus. The call blocks if a reply is diff --git a/liblazy/liblazy_dbus.c b/liblazy/liblazy_dbus.c index 2963055..9fa71af 100644 --- a/liblazy/liblazy_dbus.c +++ b/liblazy/liblazy_dbus.c @@ -25,12 +25,19 @@ #include "liblazy.h" #include "liblazy_local.h" +#include "config.h" #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <string.h> +static int dbus_system_use_private_connection = 0; + +void liblazy_dbus_system_use_private_connection(int use_private) { + dbus_system_use_private_connection = use_private; +} + int liblazy_dbus_send_method_call(const char *destination, const char *path, const char *interface, const char *method, int bus_type, @@ -47,12 +54,31 @@ int liblazy_dbus_send_method_call(const char *destination, const char *path, dbus_error_init(&dbus_error); - dbus_connection = dbus_bus_get(bus_type, &dbus_error); - if (dbus_connection == NULL || dbus_error_is_set(&dbus_error)) { - ERROR("Connection to dbus not ready, skipping method call %s: %s", - method, dbus_error.message); - ret = LIBLAZY_ERROR_DBUS_NOT_READY; - goto Free_Error; + if (bus_type == DBUS_BUS_SYSTEM && dbus_system_use_private_connection) { + dbus_connection = dbus_connection_open_private(DBUS_SYSTEM_BUS_SOCKET, + &dbus_error); + if (dbus_connection == NULL || dbus_error_is_set(&dbus_error)) { + ERROR("Connection to dbus not ready, skipping method call %s: %s", + method, dbus_error.message); + ret = LIBLAZY_ERROR_DBUS_NOT_READY; + goto Free_Error; + } + + dbus_bus_register(dbus_connection, &dbus_error); + if (dbus_error_is_set(&dbus_error)) { + ERROR("Could not register private connection, skipping method call %s: %s", + method, dbus_error.message); + ret = LIBLAZY_ERROR_DBUS_NOT_READY; + goto Free_Error; + } + } else { + dbus_connection = dbus_bus_get(bus_type, &dbus_error); + if (dbus_connection == NULL || dbus_error_is_set(&dbus_error)) { + ERROR("Connection to dbus not ready, skipping method call %s: %s", + method, dbus_error.message); + ret = LIBLAZY_ERROR_DBUS_NOT_READY; + goto Free_Error; + } } message = dbus_message_new_method_call(destination, path, interface, method); @@ -74,6 +100,10 @@ int liblazy_dbus_send_method_call(const char *destination, const char *path, } dbus_message_unref(message); + if (dbus_system_use_private_connection) { + dbus_connection_close(dbus_connection); + dbus_connection_unref(dbus_connection); + } Free_Error: dbus_error_free(&dbus_error); return ret; |