LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-14-2005, 05:11 AM   #1
Aga
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Rep: Reputation: 0
Question How to share socket between processes


I want to do followed thing.
Connect TCP socket in one process and work with it in another one.
It works fine if I fork second proc after connection establishing. But I need to to open sockets after fork().
I tried to use clone() with CLONE_FILES flag, it works. But clone() isn't POSIX api and it isn't supported by cygwin.

Is there another way to share sockets?
 
Old 02-14-2005, 06:22 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
AFAIK it's not possible.
But, of course, you can open another socket to the same effect.

Last edited by Hko; 02-14-2005 at 06:34 AM.
 
Old 02-14-2005, 06:51 AM   #3
Aga
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Original Poster
Rep: Reputation: 0
What do you mean: same effect.
 
Old 02-14-2005, 10:48 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
I meant:
Sharinga descriptor to a socket would have effectively the same effect/result if you just open another socket and connect it to the same address.
 
Old 02-14-2005, 11:43 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Use Unix domain sockets

Yes, sharing a socket between two processes *IS* possible.

You need to use a "Unix domain socket" to pass the descriptor from the process that opened the socket to the process that you want to share it. You'll also need some mechanism for synchronizing the two processes.

I'd strongly recommend this book:

Unix Network Programming, W. Richard Stevens, ISBN 0131411551

'hope that helps .. PSM
 
Old 02-15-2005, 03:13 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
You need to use a "Unix domain socket" to pass the descriptor from the process that opened the socket to the process that you want to share it.
Can you elaborate on that ?
Unix domain sockets are a way to pass *data* from a process to another.
How would you pass the socket descriptor to the receiving socket, and how this destination process can make use of this descriptor ?
 
Old 02-16-2005, 10:59 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
No, Unix domain sockets also permit you to pass *socket file descriptors* between processes.

The basic trick is to:

1. open a socket of type "AF_UNIX" (instead of the more common "AF_INET") in each process
2. Create a pipe to join the two sockets
3. Do a sendmsg() on the sender, and a recvmsg () on the process you want to share the socket

I did a brief Google search, and didn't find any URL's I could point you to.

But it does work - trust me.

And it's discussed in detail in the Stevens book I mentioned above.

'Hope that helps .. PSM

PS:
An equivalent technique is available on Windows: you need to do an OpenProcess() and a DuplicateHandle ().
 
Old 02-17-2005, 01:12 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
But it does work - trust me.

And it's discussed in detail in the Stevens book I mentioned above.
Sorry, I'm still unconvinced, sendmsg and recvmsg are just another way to pass data, and anyway the issue is not passing something, but reusing it as a socket descriptor.
 
Old 02-17-2005, 01:21 PM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Too bad. You're mistaken.

Sendmsg and Rcvmsg are indeed a means of sending data. However, they can also be used as a means of "exporting" a file descriptor (such as a socket) from one process and making it a valid file descriptor in a different, otherwise unrelated process.

Why don't you look it up (or better, try it) yourself, and share with the group what you learn? I've used this technique on other Unix's (and Windows) in years past, but I've never had the opportunity to try it on Linux. I'm definitely curious what you find.

Your .. PSM
 
Old 02-17-2005, 02:31 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Sorry, but I'm not going to spend time to try to implement something I currently think is undoable.

You are the one suggesting this can work, but up to now haven't provide any evidence of it.

If you want to be trusted, show us it can be done.

If you really did it on Unix or Windows, please share that code/technique with us. This forum is not restricted to Linux.
 
Old 02-18-2005, 02:40 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Okay, so your first search was less than 5 seconds ...
Quote:
I did a brief Google search, and didn't find any URL's I could point you to.
Anyway, sorry for not trusting you in the first time.
This is a very unadvertised feature.

I found some source code that shows how it can be implemented, and tried it under Solaris, it worked well.

Here's the link:
http://search.cpan.org/src/SAMPO/Soc...-0.03/passfd.c

Thanks
 
Old 02-18-2005, 12:35 PM   #13
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

My pleasure. And thanx for sharing link to the example code in Solaris!

Sincerely .. PSM
 
Old 02-19-2005, 12:09 AM   #14
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
I'm also interested in this, read through the example sources and still am trying to understand.
So if the fd is created in one process, say has value 3, and it's passed to another process does it still have the value 3 when it is received?
Is a new fd created somewhere, duplicated or otherwise modified, that I'm not seeing, that will be valid in the new process? Just trying to figure this out.
 
Old 02-19-2005, 02:57 AM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The file descriptor number is different, but the underlying kernel structure seems to be shared, e.g. seeking the pointer on one side is seen on the other.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
share socket between two process Vikash Singh Linux - Networking 0 10-10-2003 07:21 AM
Standard Way To Share Memory Among Processes? Sys-V IPC? overbored Programming 1 06-21-2003 01:33 PM


All times are GMT -5. The time now is 11:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration