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: File descriptor suddenly goes bad - comp.unix.programmer ...File descriptor suddenly goes bad - comp.unix.programmer ..... sending process uses the socket there is no problem. However, if I run lsof ... call will close the socket ... Bad File Descriptor - comp.protocols.time.ntpIt seems that ntpd is recording bad file descriptor errors in the system log. ... comp.parallel.mpi - page 13 mpirun, net_send write, bad file descriptor 0 150 (10/28/2003 7:47 ... detecting disconnection with select() - comp.unix.programmer ...... sending data (the network thread) and the other is > responsible for computation. > The network thread calls select() listening to a number of file > descriptors ... Controlling File Descriptor Flush - comp.unix.solarisSending an EOF to a named pipe - comp.os.linux.questions ... Controlling File Descriptor Flush - comp.unix.solaris Inorder to implement proper rollback ... an EOF to a ... socket lock in multithred programming - comp.unix.programmer ...... process. it has a tcp/ip socket and two threads, one thread for sending message ... man 7 socket man 2 close and some tutorial about unix, processes, file descriptors and ... Does directio(fd, DIRECTIO_ON) survive the file descriptor? - comp ...In general, the transfer is pos- sible when the application's buffer ... persists until the next reboot. > > Second, if any process has an open file descriptor ... Large File Transfers via UDP - Continued - Hello? - comp.os.vms ...Bad File Descriptor - comp.protocols.time.ntp Hello, I'm having an ... I have to transfer some files to a https server using ... control connection for command: Bad file ... Trouble setting max filedescriptors / shared memory / stack size ...... 09:01:13 f0x09 genunix: [ID 883052 kern.notice] basic rctl process.max-file-descriptor ... part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send ... Inputs required on 'fdx' command usage in a shell script - comp ...I have to transfer some files to a https server using fdx command and I'm writing ... 1 401 Authorization Required No control connection for command: Bad file descriptor Lost ... Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ...This will make the file-descriptor-level = I/O functions such as read() and write() use ... 5DE6FB40 Content-Type: text/html; charset="iso-8859-1" Content-Transfer ... Process sending file descriptors to another process - C BoardProcess sending file descriptors to another process. This is a discussion on Process sending file descriptors to another process within the C Programming forums, part ... File descriptor - Wikipedia, the free encyclopediafileno() Operations on a single file descriptor. read(), write() readv(), writev() pread(), pwrite() recv(), send() recvmsg(), sendmsg() (including allowing sending FDs) 7/24/2012 10:00:10 AM
|