/* * Description: Connect to a D-Bus server using its private socket address. * Author: James Hunt * Date: 14 October 2013. * gcc -std=gnu99 -ggdb -Wall -pedantic -o bin/test_nih_dbus_client test_nih_dbus_client.c `pkg-config --cflags --libs libnih libnih-dbus dbus-1` */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include char *address = NULL; char *object_path = NULL; void lost_handler (void *data, NihDBusProxy *proxy) __attribute__ ((unused)); void lost_handler (void *data, NihDBusProxy *proxy) { nih_assert (proxy); nih_message ("XXX:%s:%d:", __func__, __LINE__); } NihDBusProxy * make_proxy (void) { NihDBusProxy *proxy = NULL; DBusConnection *conn = NULL; DBusError dbus_error; assert (address); assert (object_path); dbus_error_init (&dbus_error); #if 1 #if 1 /* XXX: incorrect */ conn = dbus_connection_open (address, NULL); #else /* XXX: also incorrect */ conn = dbus_connection_open_private (address, NULL); #endif #else /* XXX: correct */ conn = nih_dbus_connect (address, NULL); #endif if (! conn) { nih_fatal ("failed to connect to address '%s': %s", address, dbus_error.message); dbus_error_free (&dbus_error); exit (EXIT_FAILURE); } proxy = nih_dbus_proxy_new (NULL, /* parent */ conn, /* connection */ NULL, /* name (NULL == peer-to-peer) */ object_path, /* path */ NULL, /* lost_handler: must be NULL if name is NULL */ NULL /* data */); nih_assert (proxy); nih_assert (proxy->connection); /* Drop our ref since nih_dbus_proxy_new() takes a ref itself */ dbus_connection_unref (conn); return proxy; } #if 0 void send_event (NihDBusProxy *proxy) { int ret; nih_assert (proxy); ret = upstart_emit_event_sync (NULL, proxy, event, NULL, 0); nih_message ("ret=%d", ret); } #endif void run_main_loop (void) { int ret; NihDBusProxy *proxy = NULL; proxy = make_proxy (); #if 0 nih_message ("sending events"); send_event (proxy); send_event (proxy); send_event (proxy); #endif ret = nih_main_loop (); nih_fatal ("main loop exited with ret %d", ret); nih_free (proxy); } int main (int argc, char *argv[]) { if (argc != 3) { nih_message ("Usage: %s
", argv[0]); nih_message ("\n"); nih_message ("(Example address: 'unix:abstract=/com/ubuntu/upstart-session/1000/2798')\n"); nih_message ("(Example object-path: '/com/hunt/james/test')"); exit (EXIT_FAILURE); } address = argv[1]; object_path = argv[2]; run_main_loop (); exit (EXIT_SUCCESS); }