I read Stevens' example on passing descriptors between processes. To summarize, his main program forks a child which exec another program, which opens a file, passes the integer fd
back to parent via unix domain socket and exit. Parent receives this fd
from the socket, and directly reads the file using the fd
.
Two questions come up:
- Parent and child are two separate processes, therefore unless they share file descriptor table(which is NOT the default
fork
behavior, sinceCLONE_FILES
is not set, AFAIK), parent wouldn't be able to use thefd
from child directly. Parent needs to find a slot in its descriptor array and map it to thefile
object made by child. Stevens mentioned the issue, but the code does not seem to do this mapping on receiving side. - The
file
object made by child will be freed upon process exit, if child does not increase refcount. Again, Stevens mentioned this in description leading up to the code but the code itself does not seem to do this.
I found a related SO post, where the role of parent and child is reversed, otherwise it is the same as Stevens' example. Not sure how that one works either.
Am I missing something here? My guesses are based on Linux, maybe unix is different enough that those two issues are somehow taken care of by the kernel? Help appreciated!