Sending file descriptors.

  • Follow


Hello,
I have the following problem, but I don't know if there is a solution
(at least I don't see it).
The short question is: is it possible to "send" a file descriptor from
a process to another? In theory, file descriptors are kernel objects,
so it should be possible to "pass" the reference to another process.
In my particular scenario, I have two processes, both dealing with
socket connections, and talking to each other via a unix domain
socket. It would be nice if the first one could pass the connected
socket to the second, so that it could remove (its copy of) the fd and
let the second complete the request. Why this is needed (other than
keep processes' workloads low and make their code simpler) it's not to
hit the EMFILE for one or both of them (so keeping the first fd open
will solve only partially the problem).
Thank you!
0
Reply FtM 2/22/2011 4:48:44 PM

FtM <fmassei@gmail.com> wrote:
> I have the following problem, but I don't know if there is a solution
> (at least I don't see it).
> The short question is: is it possible to "send" a file descriptor from
> a process to another? In theory, file descriptors are kernel objects,
> so it should be possible to "pass" the reference to another process.
> In my particular scenario, I have two processes, both dealing with
> socket connections, and talking to each other via a unix domain
> socket. It would be nice if the first one could pass the connected
> socket to the second, so that it could remove (its copy of) the fd and
> let the second complete the request. Why this is needed (other than
> keep processes' workloads low and make their code simpler) it's not to
> hit the EMFILE for one or both of them (so keeping the first fd open
> will solve only partially the problem).

Yes, you can send (a reference to an) open file descripors over
a UNIX domain socket to a different process (and then close it
on the sender's side), using the sendmsg() and recmsg() func-
tions. You can find code that demonstrates how it is done e.g.
in the example code for Michael Kerrisk's new book "The Linux
Programming Interface". The relevant files are

http://man7.org/tlpi/code/online/dist/sockets/scm_rights_send.c.html
http://man7.org/tlpi/code/online/dist/sockets/scm_rights_recv.c.html

(I guess this example code is better suited than trying to come
up with a lengthy explanation since the whole thing is a bit
more complicated than normal socket handling, involving to
send the descriptor via a "control" or "ancillary" message.)

                            Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de
0
Reply jt 2/22/2011 5:36:36 PM


In <a04556af-07b3-4773-ac2b-ffd6c4e0b379@r17g2000vbc.googlegroups.com> FtM <fmassei@gmail.com> writes:

> The short question is: is it possible to "send" a file descriptor from
> a process to another?

Yes.

http://codeidol.com/unix/advanced-programming-in-unix/Advanced-IPC/-17.4.-Passing-File-Descriptors/

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

0
Reply John 2/22/2011 5:37:13 PM

On 22 Feb, 18:36, j...@toerring.de (Jens Thoms Toerring) wrote:
> http://man7.org/tlpi/code/online/dist/sockets/scm_rights_send.c.html
> http://man7.org/tlpi/code/online/dist/sockets/scm_rights_recv.c.html

On 22 Feb, 18:37, John Gordon <gor...@panix.com> wrote:
> http://codeidol.com/unix/advanced-programming-in-unix/Advanced-IPC/-1...

Thank you both! It sounds perfect! :-)
Ciao!
0
Reply FtM 2/22/2011 5:46:47 PM

3 Replies
444 Views

(page loaded in 0.068 seconds)

Similiar Articles:













7/24/2012 10:00:10 AM


Reply: