chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.86k stars 1.21k forks source link

File descriptor passing over unix domain socket? #658

Open ghost opened 3 years ago

ghost commented 3 years ago

@hoditohod commented on Dec 5, 2018, 12:59 PM UTC:

AF_UNIX sockets (usually) have the ability to pass an open file descriptor over the socket (example), does ASIO support this?

I have an async unix domain socket server, and I'd like to send open fd's to unprivileged clients. Is it possible with asio (directly or any other way)? The AISO model makes the server so simple, I wouldn't want to change it...

This issue was moved by chriskohlhoff from boostorg/asio#175.

ghost commented 3 years ago

@djarek commented on Dec 6, 2018, 2:49 AM UTC:

Here's an example that shows how to send an fd after a fork from parent to child: https://gist.github.com/djarek/aeebfd739be1e375d6b626d6011afc9c

It should be possible to implement a regular proactor-style read/write operation using this. Note that in real code you'll need to handle EWOULDBLOCK from the send/receive functions. I'm not quite sure why wait_error doesn't work with async_wait, but I might be doing something wrong on POSIX API side :).

ghost commented 3 years ago

@hoditohod commented on Dec 6, 2018, 11:10 AM UTC:

Hi Damian! Thanks for the reply! (1st I didn't even notice, as my mailbox is loaded with your activity on Beast 😉)

As I understand you simply got the fd with native_handle() and used the synchronous POSIX API's. BTW, nice and complete example!

My server uses the proactor pattern, and pushes the buffer to the client with an asio::asyncwrite call(), so I'm particularly interested in sending the buffer+fd pair with some asio::async* method.

ghost commented 3 years ago

@djarek commented on Dec 6, 2018, 8:23 PM UTC:

I might be wrong, but ASIO doesn't support non-inline out-of-band data, because the underlying POSIX sockets don't support them:

UNIX domain sockets do not support the transmission of out-of-band data (the MSG_OOB flag for send(2) and recv(2)).

I don't think you'll be able to avoid touching the native APIs. It's up to you if you'll put the work required to make it compatible with the ASIO model (it's non-trivial, but possible).