linux - AF_UNIX 套接字 : can I pass socket handle between processes?

标签 linux sockets popen

假设我创建了一个 socketpair() 并将其中一个套接字的 句柄 传递给衍生进程 (popen),上述过程是否能够与 parent 沟通?

我看到的示例是使用 fork() 应用的,这超出了我当前项目的范围。

更新:我尝试了一个简单的测试:

  1. 客户端:socketpair with sockets[0]

  2. 从客户端使用带套接字的posix_spawn 1作为命令行参数

  3. 客户端:write 到套接字...客户端在没有任何警告的情况下退出...

看来这个方法有问题。

更新:我也发现了这个 note :

管道和套接字对仅限于具有共同祖先的进程之间的通信。

最佳答案

execve 的手册页指出:

 File descriptors open in the calling process image remain open in the new
 process image, except for those for which the close-on-exec flag is set
 (see close(2) and fcntl(2)).  Descriptors that remain open are unaffected
 by execve().

由于像 popen 这样的函数是基于 execve 的,因此您从 socketpair 函数获得的文件描述符在两个进程中都应该是好的,我不明白为什么您不能以您喜欢的任何方式传递描述符。我假设在这种情况下,您的意思是将其转换为字符串并通过 STDIN 将其设置为子进程,这会将其转换回 int 以用作文件描述符。

当然值得为它编写一些试用代码。

关于linux - AF_UNIX 套接字 : can I pass socket handle between processes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1683246/

相关文章:

linux - 在 Linux 中运行查询以选择 CSV'S

java - 当另一个客户端发送到服务器时,如何让 UDP 服务器推送到客户端

python - 从 python 在 shell 中启动外部 shell python 实例

python-2.7 - Python,从运行命令获取状态

python - 没有从失败的 subprocess.Popen 获得任何错误消息

能够模拟非虚拟方法和 C 函数的 C++ 模拟框架

linux - 在 for 循环 |bash 中为变量赋值时出现问题

linux - 有没有办法告诉 sed 忽略符号链接(symbolic link)?

Node.js 请求模块获取 ETIMEDOUT 和 ESOCKETTIMEDOUT

java - UDP 多套接字