I'm writing a shell and try on implement process substitution. fork inherit of all filedescriptor, allocated memory etc. I understood that execve should keep also this kind of information, and so keep each opened filedescriptor, whenever O_CLOEXEC flag is not set.
I tried a simple python script :
fd.py :
#!/usr/bin/env python3
import sys, os
if __name__ == "__main__":
if len(sys.argv) == 1:
new_fd = open("the_content_file", "w+")
print("father table : ", os.listdir("/dev/fd"))
if os.fork() == 0:
os.execve("/PATH/OF/SCRIPT/fd.py", ["fd", "content"], os.environ)
else:
print("child table : ", os.listdir("/dev/fd"))
pass
and as output, I get :
father table : ['0', '1', '2', '3', '4']
child table : ['0', '1', '2', '3']
After the fork, I keep the same fd table, but whenever I'm using execve on an executable, I lost all and get the fd opened by default. Why the opened fd is disappearing ? Thanks