ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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 ?
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 ().
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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.
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.