[avahi] Integrating Avahi into an application with "select" in the main loop

Lennart Poettering lennart at poettering.net
Sun Aug 17 14:21:59 PDT 2008

On Fri, 08.08.08 16:40, Nathan Huesken (avahi at lonely-star.org) wrote:

> > You have several options that are far more elegant:
> > 
> > 1) Implement the AvahiPoll structure for your existing event loop,
> >    i.e. implement the seven functions defined here:
> > 
> >    http://avahi.org/download/doxygen/struct_avahi_poll.html
> > 
> >    Then, pass this struct to avahi_client_new() and the avahi client
> >    will call those functions whenever there is a new FD or timeout to
> >    sleep on. You should then add a new item to your "struct pollfd"
> >    array (in case you uses poll()) or fd_set (in case you use
> >    select()) everytime the watch_new() function you implemented is
> >    called by the avahi client libraries, and similarly for the other
> >    six functions you'd need to implement.
> Sound good! I have a question to this methode:
> Avahi also tells me what events to wait for, these are.
> So exactly the events "poll" deals with.
> For compatibility reasons I do not want to use poll but select.
> So, I put all the POLLOUT event sockets in the "writefds" and all the others in the "readfds".
> Select should return on connection closed or connection error for
> any socket in the "readfds", correct?

POLLHUP is different from both POLLIN/POLLOUT. I.e. you can have a
socket around, sleep on it for neither r nor w, but be notified when
it terminates. You cannot do this with select().

However, the Avahi client doesn't rely on this. You can ignore SIGHUP
completely. POLLPRI should map to exceptfd. 

POLLHUP is difficult to emulate. You might be able to do it with recv
and PEEKMSG, but as mentioned you don't really need tot do thus.

There are several implementations of poll() on top of select()
available. This one for example even works on Win32:


You might want to integrate a few ideas from this code into yours.


Lennart Poettering                        Red Hat, Inc.
lennart [at] poettering [dot] net         ICQ# 11060553
http://0pointer.net/lennart/           GnuPG 0x1A015CC4

More information about the avahi mailing list