简体   繁体   English

使用UNIX套接字将文件描述符从一个程序传递到同一主机上的另一个程序

[英]Passing file descriptor from one program to another on the same host using UNIX sockets

I have two prgrams lets say prog1 and prog2. 我有两个prgrams让我们说prog1和prog2。 I am opening a file with prog1 and doing some operations on it. 我正在用prog1打开一个文件并对其进行一些操作。 Now without closing the file in prog1 i am sending its file descriptor to prog2 using unix sockets which then does some operations in it. 现在没有关闭prog1中的文件,我使用unix套接字将其文件描述符发送到prog2,然后在其中执行一些操作。

Though i get the same descriptor i passed in prog1 but doing a fstat() on the fd recieved in prog2 throws an error saying Bad file descriptor. 虽然我得到了相同的描述符,我在prog1中传递但是在prog2中收到的fd上执行fstat()会抛出错误,说错误的文件描述符。 I have opened the file in prog1 with corerct permissions that is read and write for all, still i get an error. 我已经在prog1中打开了具有corerct权限的文件,这些权限对所有人都是读写的,但我仍然收到错误。

Why is it happening so. 为什么会这样。 If my way of passing a file descriptor is wrong then please suggest a correct one. 如果我传递文件描述符的方式是错误的,那么请建议一个正确的方法。

I believe this site has what you're looking for: 我相信这个网站有你想要的东西:

http://www.lst.de/~okir/blackhats/node121.html http://www.lst.de/~okir/blackhats/node121.html

There's also information in Linux's man 7 unix on using SCM_RIGHTS and other features of Unix sockets. 在Linux的man 7 unix中也有使用SCM_RIGHTS和Unix套接字的其他功能的信息。

Fix for broken link: http://web.archive.org/web/20131016032959/http://www.lst.de/~okir/blackhats/node121.html 修复损坏的链接: http//web.archive.org/web/20131016032959/http//www.lst.de/~okir/blackhats/node121.html

This is normal. 这很正常。 Each program has its own file descriptors. 每个程序都有自己的文件描述符。

EDIT : Well, it seems that you can pass file descriptor using local socket. 编辑 :好吧,似乎你可以使用本地套接字传递文件描述符。

You can see them in /proc/PID/fd , they are often symlinks to your files. 您可以在/proc/PID/fd看到它们,它们通常是文件的符号链接。 What you can do with unix socket is allowing a write to a file from one program to another with sendmsg/recvmsg. 你可以用unix socket做什么,允许用sendmsg / recvmsg从一个程序写到另一个程序。 See this question for more detail. 有关详细信息,请参阅此问题

But there's way many better way to write concurrently to a file. 但是有很多更好的方法可以同时写入文件。 You can use fifo, shm or even just pass your offset position between your 2 programs. 您可以使用fifo,shm甚至只是在两个程序之间传递偏移位置。

A file descriptor is a small int value that lets you access a file. 文件描述符是一个小的int值,可用于访问文件。 It's an index into a file descriptor table , a data structure in the kernel that's associated with each individual process. 它是文件描述符表的索引, 文件描述符表是内核中与每个单独进程相关联的数据结构。 A process cannot do anything meaningful with a file descriptor from another process, since it has no access to any other process's file descriptor table. 进程无法对来自其他进程的文件描述符执行任何有意义的操作,因为它无权访问任何其他进程的文件描述符表。

This is for basic security reasons. 这是出于基本安全原因。 If one process were able to perform operations on an open file belonging to another process, chaos would ensue. 如果一个进程能够对属于另一个进程的打开文件执行操作,则会发生混乱。 Also, a file descriptor just doesn't contain enough information to do what you're trying to do; 此外,文件描述符不包含足够的信息来执行您要执行的操作; one process's file descriptor 0 (stdin) might refer to a completely different file than another process's file descriptor 0. And even if they happen to be the same file, each process needs to maintain its own information about the state of that open file (how much it's read/written, etc.). 一个进程的文件描述符0(stdin)可能引用一个完全不同于另一个进程的文件描述符0的文件。即使它们碰巧是同一个文件,每个进程也需要维护自己的有关该文件的状态信息(如何很多它的读/写等)。

If you'll describe what you're trying to accomplish, perhaps we can help. 如果你要描述你想要完成的事情,也许我们可以提供帮助。

EDIT : 编辑:

You want to pass data from one program to another. 您希望将数据从一个程序传递到另一个程序。 The most straightforward way to do this is to create a pipe ( man 2 pipe ). 最直接的方法是创建一个管道( man 2 pipe )。 Note that the second process will have to be a child of the first. 请注意,第二个进程必须是第一个进程的子进程。

An alternative might be to create a file that the second process can open and read (not trying to share a file descriptor), or you might use sockets. 另一种方法是创建第二个进程可以打开和读取的文件(不尝试共享文件描述符),也可以使用套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM