#include <stdio.h>
#include <stdlib.h>
#include "dbus/dbus.h"

void
get_dbus_pid (DBusConnection *bus)
{
  DBusMessage *message, *reply;
  dbus_uint32_t pid;
  DBusError error;
  const char *name = dbus_bus_get_unique_name (bus);

  dbus_error_init (&error);
  message = dbus_message_new_method_call ("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetConnectionUnixProcessID");
  dbus_message_append_args (message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
  reply = dbus_connection_send_with_reply_and_block (bus, message, -1, &error);
  if (!reply)
  {
    fprintf(stderr, "Sorry; dbus wouldn't answer me: %s\n", error.message);
    exit(1);
  }
  dbus_message_get_args (reply, NULL, DBUS_TYPE_UINT32, &pid, DBUS_TYPE_INVALID);
  dbus_message_unref (reply);
  dbus_message_unref (message);
}

main(int argc, const char *argv[])
{
  DBusConnection *bus;
  DBusError error;
  int i;

  dbus_error_init (&error);
  bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
  if (!bus)
  {
    fprintf(stderr, "Couldn't find the bus stop: %s\n", error.name);
    return 1;
  }
  for (i = 0; i < 20000; i++)
  {
    get_dbus_pid (bus);
  }
}
