Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I open a socket and pass it to another process in Linux

Tags:

linux

sockets

In Linux, is it possible for me to open a socket and pass the socket to another process? If yes, can you please tell me where I can find an example?

Thank you.

like image 567
n179911 Avatar asked Jan 04 '10 05:01

n179911


People also ask

Can two processes share a socket?

You can share a socket between two (or more) processes in Linux and even Windows.

How many sockets can a process have?

For most socket interfaces, the maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.

How sockets work in Linux?

Sockets are a way to enable inter-process communication between programs running on a server, or between programs running on separate servers. Communication between servers relies on network sockets, which use the Internet Protocol (IP) to encapsulate and handle sending and receiving data.

Are sockets processes?

In short, a process sends messages into, and receives messages from, the network through a software interface called a socket. A process is a program that is running within an end system.


1 Answers

Yes you can, using sendmsg() with SCM_RIGHTS from one process to another:

SCM_RIGHTS - Send or receive a set of open file descriptors from another process. The data portion contains an integer array of the file descriptors. The passed file descriptors behave as though they have been created with dup(2).

http://linux.die.net/man/7/unix

That is not the typical usage though. More common is when a process inherits sockets from its parent (after a fork()). Any file handles (including sockets) not closed will be available to the child process. So the child process inherits the parent's sockets.

A server process that listens for connections is called a daemon. This usually forks on each new connection, spawning a process to handle each new request. An example of the typical daemon is here:

http://www.steve.org.uk/Reference/Unix/faq_8.html#SEC88

Scroll down to void process().

like image 133
jspcal Avatar answered Oct 05 '22 01:10

jspcal