#define DBUS_API_SUBJECT_TO_CHANGE #include #include #include #include bool keeprunning; pthread_t thread; pthread_mutex_t lock; void* serverthread(void*) { DBusConnection* conn; DBusError err; int ret; printf("Listening for method calls\n"); // initialise the error dbus_error_init(&err); // connect to the bus and check for errors conn = dbus_bus_get(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Connection Error (%s)\n", err.message); dbus_error_free(&err); return false; } dbus_connection_close(conn); dbus_connection_unref(conn); dbus_shutdown(); return 0; } void startserver() { pthread_mutex_init(&lock, NULL); pthread_create(&thread, 0, serverthread, 0); } void stopserver() { pthread_mutex_lock(&lock); keeprunning = false; pthread_mutex_unlock(&lock); pthread_join(thread, 0); pthread_mutex_destroy(&lock); } void clientthread() { sleep(1); } int main() { startserver(); // run client code clientthread(); // stop server stopserver(); return 0; }