简体   繁体   中英

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

I have two prgrams lets say prog1 and prog2. I am opening a file with prog1 and doing some operations on it. 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.

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. I have opened the file in prog1 with corerct permissions that is read and write for all, still i get an error.

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

There's also information in Linux's man 7 unix on 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

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. What you can do with unix socket is allowing a write to a file from one program to another with 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.

A file descriptor is a small int value that lets you access a file. 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.).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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