gspawn.c file descriptor handling
Submitted by Morten Welinder
I am having a hard time convincing myself that gspawn handles file descriptors correctly.
Assume that one or more of fds 0,1,2 are closed before any of the public APIs are called. Since Unix allocates descriptors on a lowest-first basis, these will likely be allocated by gspawn.c's pipe/open.
Then do_exec will unexpectedly close them when it does "sane_dup2 (stdin_fd, 0)". (That call closes fd 0 unless stdin_fd is already zero.) Not good.
Of course, if I could find a case that fails, it would be better, :-(
FYI, Emacs contains code to avoid this problem. Basically, file descriptors created using pipe or open are relocated up above 2 using dup.