#include <stdio.h> #include <glib.h> #include <dbus/dbus.h> #include <dbus/dbus-glib-lowlevel.h> static DBusHandlerResult filter_function (DBusConnection *connection, DBusMessage *message, void *user_data) { fprintf (stderr, "obj_path=%s interface=%s method=%s\n", dbus_message_get_path(message), dbus_message_get_interface(message), dbus_message_get_member(message)); if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") && strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0) { /* this is a local message; e.g. from libdbus in this process */ fprintf (stderr, "Got disconnected from the system message bus\n"); dbus_connection_unref (connection); } return DBUS_HANDLER_RESULT_HANDLED; } int main (int argc, char *argv[]) { GMainLoop *loop; DBusError error; DBusConnection *connection; loop = g_main_loop_new (NULL, FALSE); dbus_error_init (&error); if ((connection = dbus_bus_get (DBUS_BUS_SESSION, &error)) == NULL) { fprintf (stderr, "dbus_bus_get(): %s\n", error.message); goto out; } dbus_connection_setup_with_g_main (connection, NULL); dbus_connection_set_exit_on_disconnect (connection, FALSE); dbus_bus_request_name (connection, "org.test.TestService", 0, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "dbus_bus_request_name(): %s\n", error.message); goto out; } dbus_connection_add_filter (connection, filter_function, NULL, NULL); g_main_loop_run (loop); out: return 0; }