_dbus_write/read/poll etc and regular files

Tor Lillqvist tml@iki.fi
Mon Jan 17 06:17:38 PST 2005


Hi,

How important is it that _dbus_write(), _dbus_poll() etc can be used
also for file descriptors referring regular files, not just sockets?

If I have read the current code in CVS correctly, the only use case
with non-sockets is the writing of the message bus address and process
pid in bus_context_new() and _dbus_become_daemon() when called from
bus/main.c:main(). Would it be better to use "normal" write(), or even
printf() instead for this?

The reason is that on Win32, file descriptors (the stuff returned by
open(), fileno() etc) are local to the C library. WinSock sockets
don't have anything to do with C library file descriptors. This can be
a bit of a problem in porting Unix code that assumes sockets are file
descriptors, they can all be select()ed, etc.

(Look at GLib on Win32, which has horrendous hacks (I can say that as
I wrote them ;-) to get around this, with separate reader threads and
whatnot, and still it doesn't work in all use cases.)

To get past this, I have added a "wrapper" or "encapsulation" layer in
dbus-sysdeps.c so that one can encapsulate either a C library file
descriptor or WinSock socket, and get back a D-BUS-private "pseudo"
file descriptor, which then is passed to _dbus_write()
etc. _dbus_write() then either uses write() or send(). _dbus_read()
either read() or recv(). _dbus_poll() works only for sockets.

But it would be simpler if I would know that only sockets need to be
handled, this extra layer could then be dropped.

(More info: So far, my idea is to use localhost-bound TCP sockets for
the D-BUS communication, and thus must of the socket-related code can
be reused on Windows. Named pipes and so-called overlapped IO would
perhaps be more "native", but handling them would require very
different code, and take more time to write and debug. To make the TCP
socket usage more like Unix domain sockets on Unix, I store the
anonymous port number in a file which mimics the Unix domain socket
"file", so that one doesn't have to choose fixed port numbers.)

Once I have everything compiling and dbus-test and bus-test working
again after Havoc's recent commits, I'll put up diffs for people to
check.

--tml




More information about the dbus mailing list