Detection of dangling file handles/FDs
by quiret from LinuxQuestions.org on (#59D4D)
Dear linux developers,
let's assume I have a file called /media/quiret/INTENSO/test.txt. My program opens a fd to it using open("/media/quiret/INTENSO/test.txt", O_RDONLY ). The folder /media/quiret/INTENSO/ is actually my USB drive. Then I forget the worries of the world and plug out my USB drive while the FD is open in my program. Now I have several questions:
1. can my program detect that the file is missing after the atomic call to read/write/etc?
2. how does Linux read data from the file if the file has no links?
3. how long does a fd stay valid for file operations if it has no more links? and when does the first error get returned by any FS syscall?
Thank you for your time!
- quiret/rplgn/The_GTA


let's assume I have a file called /media/quiret/INTENSO/test.txt. My program opens a fd to it using open("/media/quiret/INTENSO/test.txt", O_RDONLY ). The folder /media/quiret/INTENSO/ is actually my USB drive. Then I forget the worries of the world and plug out my USB drive while the FD is open in my program. Now I have several questions:
1. can my program detect that the file is missing after the atomic call to read/write/etc?
2. how does Linux read data from the file if the file has no links?
3. how long does a fd stay valid for file operations if it has no more links? and when does the first error get returned by any FS syscall?
Thank you for your time!
- quiret/rplgn/The_GTA