views:

223

answers:

4

I would like to implement a socket-like object in user space. There's an important requirement that it should be pollable (i.e. it's state should be queryable via select or poll call).

Is there a platform neutral way of implementing such an object?

I'm aware that on Linux there's eventfd which kind of suits the needs except that there's no way to force it to signalize neither POLLIN nor POLLOUT.

+1  A: 

You want to build an user space object, that will be accessible through system call ? ie open, read, write etc ... are redirected to your userspace object ?

You need either kernel support or libc support, otherwise I don't see how you can redirect your system call.

eventfd is not what you are asking for, it is implemented in kernel space. Did you describe your real problem ? Could fifo or unix domain socket fit your need ?

What about pseudo tty ? I don't know if you can block writing from the master side by faking the hardware flow control.

shodanex
I just want to be able to interrupt the poll call with either POLLIN or POLLOUT or both. Is there a way to accomplish that?
A: 

It's really not clear what you're trying to do; if you want a socket-like device, why not use sockets? You don't say ... And what's the deal with POLLIN and POLLOUT?

I kinda suspect you might be interested in using pseudo-terminal devices, see man 7 pty.

niXar
A: 

Use pipe(). It gives you two fd's, one to write, one to read. Use the fd[1] to do your select/poll on.
Use the fd[0] to signal your select/poll for activity.

codeDr
+1  A: 

You can use socketpair() to create a pair of connected AF_UNIX sockets. This is better than pipe() as it allows for bidirectional communication. If this isn't good enough for your needs, another option (which requires root for a daemon) would be to use the as-yet-not-in-mainline-Linux CUSE patches to create a device driver in userspace to do whatever you like. Or you can just hook into whatever event loop your user will be using...

The new linux eventfd can also emulate POLLIN/POLLOUT, although not both at once - set its value to 0xfffffffffffffffe for POLLIN but not POLLOUT, 0 for POLLOUT but not POLLIN, or anything else for both.

Other than these options, there's no platform-neutral way to do this, no. The usual pattern is to use a FIFO just to wake up the event loop, and have it poll using some other API once it's awake.

bdonlan
The problem is that socketpair doesn't allow for simulating OUT event. I can cause POLLIN on the other end of the socketpair by writing a single byte to the local end, however, I have no way to switch POLLOUT on/off on the remote end.
Unfortunately you're a bit stuck then. Perhaps you should propose a new API on lkml or something?
bdonlan