我可以打开一个套接字并将它传递到Linux中的另一个进程吗

时间:2021-04-02 23:58:06

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?

在Linux中,我是否可能打开一个套接字并将套接字传递到另一个进程?如果是,你能告诉我在哪里可以找到一个例子吗?

Thank you.

谢谢你!

1 个解决方案

#1


55  

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

是的,您可以使用sendmsg()和SCM_RIGHTS从一个进程到另一个进程:

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).

scm_right——从另一个进程发送或接收一组打开的文件描述符。数据部分包含文件描述符的整数数组。通过的文件描述符的行为就好像它们是用dup(2)创建的一样。

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

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.

但这不是典型的用法。更常见的情况是进程从其父进程继承套接字(在fork()之后)。任何文件句柄(包括套接字)都不会关闭,这将用于子进程。子进程继承父进程的套接字。

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

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

Scroll down to void process().

向下滚动到void process()。

#1


55  

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

是的,您可以使用sendmsg()和SCM_RIGHTS从一个进程到另一个进程:

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).

scm_right——从另一个进程发送或接收一组打开的文件描述符。数据部分包含文件描述符的整数数组。通过的文件描述符的行为就好像它们是用dup(2)创建的一样。

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

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.

但这不是典型的用法。更常见的情况是进程从其父进程继承套接字(在fork()之后)。任何文件句柄(包括套接字)都不会关闭,这将用于子进程。子进程继承父进程的套接字。

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

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

Scroll down to void process().

向下滚动到void process()。