Tracking DBusWatches

tsuraan tsuraan at gmail.com
Mon Feb 5 12:16:53 PST 2007


I'm working on writing a mainloop, mostly just trying to figure out what's
needed.  It looks like for an asynchronous program using dbus, one should
really use the DBusWatch structures, so that's what I'm trying to figure
out.  My issue right now is what memory is mine to manage, and what is owned
and managed by the dbus library.  In my trivial example program (listed
below), I just connect to the bus and call
dbus_connection_set_watch_functions with my stub functions to see what
happens.  My program prints out every time a watch is added, removed,
toggled, and deleted.  Through the run of my program, two watches are added,
none are removed, toggled, or deleted.  Both added watches have the same
associated file descriptor, but different addresses.  The first call is
given a disabled watch, and the second is given an enabled one.

Since the watches are supposed to be used for select(), I assume that it
doesn't make sense to keep track of repeated watch additions that have the
same file descriptor.  Should I just free redundant watches, or is dbus
doing that behind my back?  Or, if I were to write a somewhat less trivial
program, would the out-of-date watch be eventually given to the remove and
free functions that were registered with
dbus_connection_set_watch_functions?

Code listing:

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

dbus_bool_t addWatch(DBusWatch *w, void *d)
{
  int fd = dbus_watch_get_fd(w);
  int e  = dbus_watch_get_enabled(w);

  printf("Adding watch with address %d\n", (int)w);
  printf(" - fd is %d\n", fd);
  printf(" - state is %s\n", e?"enabled":"disabled");
}

void delWatch(DBusWatch *w, void *d)
{
  printf("Deleting watch at %d\n", (int)w);
}

void toggleWatch(DBusWatch *w, void *d)
{
  printf("Toggling watch at %d\n", (int)w);
}

void mfree(void *data)
{
  printf("Freeing data at %d\n", (int)data);
  dbus_free(data);
}

int main() {
  DBusError err;
  DBusConnection* conn;

  dbus_error_init(&err);
  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);
  }
  if (NULL == conn) {
    exit(1);
  }

  if(FALSE == dbus_connection_set_watch_functions(conn, addWatch, delWatch,
toggleWatch, 0, dbus_free)) {
    fprintf(stderr, "Couldn't set up watch functions\n");
    exit(1);
  }

  exit(0);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20070205/e1093d1d/attachment.html


More information about the dbus mailing list