Passing a file descriptor to a parent process
Is there a way for a child process to open a file descriptor and let the parent process access it directly?
Here's what I'm trying to do:
My main process spawns a SUID executable that mounts a filesystem to a temporary directory, opens a file descriptor of the mountpoint (directory) and does a lazy umount and removes the directory. I want the parent process to be able to fchdir to the directory that the SUID program opened.
I could notify the parent using signals or even pipes but I fear that would increase the likelyhood of errors and thus the likelyhood of the mounted directory being left in the temporary directory, which would be dangerous (imagine rm -Rf /tmp/*).
In principle you can pass a file descriptor to any process using Unix sockets (which can also be conveniently created in the parent with sockepair() I think)
Look at the socketpair man page, and unix(7) man page.
I'm not sure if this is Linux-specific or can be done on most Unix.
So in summary
- In the parent, create a socket pair with socketpair()
- Pass one of the pair to the child in the normal way
- Have the child call sendmsg() to pass the file descriptor back to the parent
- Child can now exit, parent has file descriptor.
I haven't tried this, but it should work.
Mark
Look at the socketpair man page, and unix(7) man page.
I'm not sure if this is Linux-specific or can be done on most Unix.
So in summary
- In the parent, create a socket pair with socketpair()
- Pass one of the pair to the child in the normal way
- Have the child call sendmsg() to pass the file descriptor back to the parent
- Child can now exit, parent has file descriptor.
I haven't tried this, but it should work.
Mark
I don't think I understand. Isn't sendmsg for passing arbitrary data? If I pass the file descriptor integer to the parent, will it still be valid in the parent?
Child processes seem to inherit the parents descriptors but how can I make it work the other way?
Child processes seem to inherit the parents descriptors but how can I make it work the other way?
Read the man page for unix(7) and look up the section ANCILLARY MESSAGES.
Specifically,
Mark
Specifically,
ANCILLARY MESSAGES Ancillary data is sent and received using sendmsg(2) and recvmsg(2). For historical reasons the ancillary message types listed below are specified with a SOL_SOCKET type even though they are PF_UNIX specific. To send them set the cmsg_level field of the struct cmsghdr to SOL_SOCKET and the cmsg_type field to the type. For more information see cmsg(3). SCM_RIGHTS Send or receive a set of open file descriptors from another pro- cess. 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).
Mark
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement