1

Is there any way to create a new process which shares your file descriptor table even after an exec? clone(CLONE_FILES) won't work, as the man page says:

If a process sharing a file descriptor table calls execve(2), its file descriptor table is duplicated (unshared).

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • 2
    What's the goal here? Why would you want a live, shared copy of the file descriptor table once the processes stop even being the same executable? It seems nonsensical at first glance, so I'm suspicious this may be [an XY problem](https://meta.stackexchange.com/q/66377/322040). – ShadowRanger Jun 14 '17 at 01:32
  • 1
    @ShadowRanger I'm writing an x86 user mode emulator for Linux and testing it by running a real process under ptrace at the same time as the emulator and comparing the cpu state at each instruction. When it's time to emulate an mmap call, I want to take the FD used in the parent and overwrite edi to use it in the child. Not a very common use case, that's for sure. – tbodt Jun 14 '17 at 01:36
  • Even though the open file descriptors are not shared after the `execve()`, the open file descriptions are still shared — yes, there really are descriptors and descriptions. Read the POSIX specifications of [`open()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html), [`dup2()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html), [`fork()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html), [`execve()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/execve.html) carefully. – Jonathan Leffler Jun 14 '17 at 02:40
  • @JonathanLeffler I'm talking about the descriptors, not the descriptions. I want an `open` in the parent process to change the file descriptor table of the child process. – tbodt Jun 14 '17 at 02:42
  • Nothing doing, then — can't be done using POSIX system calls. If you want that level of sharing, you should be using threads instead of processes. – Jonathan Leffler Jun 14 '17 at 02:43
  • 1
    @JonathanLeffler I don't care about POSIX, I just want it to work on Linux – tbodt Jun 14 '17 at 02:43
  • Then you'll have to wait for someone else to tell whether it can be done, but I'd be moderately surprised if it can. I'm not convinced I can see a use case for it. – Jonathan Leffler Jun 14 '17 at 02:44
  • 1
    @JonathanLeffler I'm getting the feeling I'm the only one in the world who actually wants to do this... – tbodt Jun 14 '17 at 02:45
  • 2
    @tbodt You probably are the only person who wants to do it, and for some reason some people will assume that makes you an idiot. – user253751 Jun 14 '17 at 03:39
  • @immibis I've described the root problem I'm trying to solve in the second comment, if anyone has any better ideas I'd be glad to hear them – tbodt Jun 14 '17 at 03:40
  • @immibis and idiots are so happy when they find each other... – sqr163 Jun 15 '17 at 19:27

1 Answers1

2

This can be done by injecting into the child process a custom piece of code responsible for the receival of FDs and updating the child's descriptor table.

Child process should create AF_UNIX socket and recvmsg() on it, while parent process should duplicate and "stream down" the required file descriptors using sendmsg() - see here

sqr163
  • 1,074
  • 13
  • 24