Transferring a file descriptor from one program to another on the same host using UNIX sockets

I have two programs that say prog1 and prog2. I open the file with prog1 and perform some operations on the topic. Now, without closing the file in prog1, I send its file descriptor to prog2 using unix sockets, which then perform some operations on it.

Although I get the same descriptor that I passed to prog1, but I execute fstat () on fd, the one received in prog2 gives an error message "Bad file descriptor". I opened the file in prog1 with corerct permissions that is read and written for everyone, however I get an error message.

Why it happens. If my method of transferring a file descriptor is incorrect, please suggest the correct one.

+4
source share
3 answers

I believe that this site has what you are looking for:

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

There is also information on Linux man 7 unix about using SCM_RIGHTS and other features of Unix sockets.

Fix for broken link: http://web.archive.org/web/20131016032959/http://www.lst.de/~okir/blackhats/node121.html

+7
source

This is normal. Each program has its own file descriptors.

EDIT . Well, it looks like you can pass the file descriptor using a local socket.

You can see them in /proc/PID/fd , they are often symbolic links to your files. What you can do with a unix socket allows you to write a file from one program to another using sendmsg / recvmsg. See this question for more details.

But there are many ways to record simultaneously with a file. You can use fifo, shm, or even just pass your offset position between two programs.

+4
source

A file descriptor is a small int value that allows you to access the file. This is the index to the file descriptor table, the data structure in the kernel associated with each individual process. A process cannot do anything meaningful in a file descriptor from another process because it does not have access to any other process file descriptor table.

This is due to basic security considerations. If one process could perform operations on an open file belonging to another process, chaos would arise. In addition, the file descriptor simply does not contain enough information to do what you are trying to do; one process file descriptor 0 (stdin) can refer to a completely different file than another process file descriptor 0. And even if they are the same file, each process must maintain its own information about the state of this open file (how much it read / wrote, etc.).

If you describe what you are trying to accomplish, perhaps we can help.

EDIT:

You want to transfer data from one program to another. The easiest way to do this is to create a channel ( man 2 pipe ). Note that the second process must be a child of the first.

An alternative could be to create a file that the second process can open and read (not try to exchange a file descriptor), or you can use sockets.

+2
source

Source: https://habr.com/ru/post/1403949/


All Articles