[Boost-users] [asio] Problem Assignin a File Descriptor to posix:basic_stream_descriptor

835 views
Skip to first unread message

Nader Salehi

unread,
Mar 17, 2010, 5:42:45 PM3/17/10
to boost...@lists.boost.org
Hi All,

I want to use Boost ASIO as an event manager in a server which, among
other things, asynchronously writes into a file. I use open() to
create my file and then try to assign it to a posix::stream_descriptor
object. However, the following simple code throws an "Operation not
permitted" exception.

int
main ()
{
boost::asio::io_service ioService;
int fd = open("./asio_sample.txt",
O_WRONLY | O_CREAT | O_ASYNC | O_NONBLOCK,
S_IRUSR | S_IWUSR | S_IRGRP);
assert(fd != -1);
boost::asio::posix::stream_descriptor descriptor(ioService);
descriptor.assign(fd);

return 0;
}

I traced down the epoll_reactor::register_descriptor() where the
function fails to add the descriptor to the poll list.

Any idea what I am doing wrong? I would appreciate any comment.

Cheers,
Nader
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Nick Jones

unread,
Mar 18, 2010, 12:19:26 AM3/18/10
to boost...@lists.boost.org
On Wed, 2010-03-17 at 14:42 -0700, Nader Salehi wrote:

> I traced down the epoll_reactor::register_descriptor() where the
> function fails to add the descriptor to the poll list.
>
> Any idea what I am doing wrong? I would appreciate any comment.
>

Filesystem fds do not support epoll, as they lack the poll function
internally in the linux kernel. otoh, select does supports filesystem
fds.

If you do an strace of your sample program, you will see that the call
to epoll_ctl fails with the error number EPERM. Yes, the value chosen
is quite unfortunate, I always thought it would have been better to
return ENOTSUP or EOPNOTSUPP... anyhow, that means that epoll is
rejecting the fd you supplied, in this case because it was a filesystem
fd and in Linux, this type of fd lacks a poll function pointer in its
file_operations structure. I remember looking through the epoll sources
in an older version of the kernel, and finding that this was the only
reason that EPERM is returned as an error.

You may have to find another method of managing IO from and to the file,
perhaps you can use another standard or asio provided stream type?

Thanks

Nick

Nader Salehi

unread,
Mar 18, 2010, 1:22:32 PM3/18/10
to boost...@lists.boost.org
I see. At this time the path of least resistance would be to use
libevent which allows me --albiet awkwardly via setenv()-- to choose
the polling method.

Thanks for the reply.

Nader

Nader Salehi

unread,
Mar 18, 2010, 4:54:31 PM3/18/10
to boost...@lists.boost.org
Correction! There's actually a better solution than what I said in my
previous reply; You could explicitly disable 'epoll' in asio and
switch to 'select' by defining BOOST_ASIO_DISABLE_EPOLL.

Cheers,
Nader

Reply all
Reply to author
Forward
0 new messages