5

I have file descriptor and inside my signal handler, i close the file. But due to other conditions, the file could have been closed earlier. Is there a way to check if the file descriptor points to an open file in c and linux?

UPDATE: Is it possible to determine filename associated with a file descriptor? This way if the fd gets recycled, app can detect it.

Jimm
  • 8,165
  • 16
  • 69
  • 118
  • 4
    possible duplicate of [How to check if a given file descriptor stored in a variable is still valid?](http://stackoverflow.com/questions/12340695/how-to-check-if-a-given-file-descriptor-stored-in-a-variable-is-still-valid) – user229044 Jan 15 '13 at 18:00
  • You mean still open by the same process? Easiest way is to set the FD to some null value when you close the file – Martin Beckett Jan 15 '13 at 18:01
  • 2
    Why not just `close` it anyway and ignore `EBADF`? If you know, that nothing is opened on this descriptor in the meantime then it should work. – zch Jan 15 '13 at 18:05
  • You can't positively determine filename associated with a descriptor. There may not be any filename associated. – mpez0 Jan 15 '13 at 21:25
  • This seems to be the opposite to http://stackoverflow.com/questions/12340695/how-to-check-if-a-given-file-descriptor-stored-in-a-variable-is-still-valid but the answer given there would work for you. – Mawg says reinstate Monica Oct 23 '15 at 14:28

3 Answers3

5

Try to do any file operation like lseek on the file descriptor. If it returns -1. Then check errno, if its EBADF then the file descriptor is already closed.

Try to do lseek in an unaffected manner like below.

lseek(fd, 0, SEEK_CUR);

Usually while opening the first file in a program we always get the file descriptor as 3. After this file is closed, if we try to open some other file we will get the same file descriptor as 3. Because always we will get the lowest available number. If we are closing and reopening many files in a program, then we need to improve our code to track of file descriptors list to check whether its closed or not.

rashok
  • 12,790
  • 16
  • 88
  • 100
2

When you open a file, it always get the minimal available fd assigned. So if you close your fd, and then open another file somewhere in your code, you could easily have the same fd reassigned to this new file. So there is no reliable way to tell that the file descriptor is closed, because it can now point to another opened file.

Xeor
  • 1,846
  • 1
  • 12
  • 12
  • Is it possible to determine if a open fd still points to a particular file? Or is it possible to determine the file name associated with fd? – Jimm Jan 15 '13 at 18:43
  • I can think of looking into /proc/self/fd in Linux, but don't consider it reliable (and definitely not portable) way. – Xeor Jan 15 '13 at 18:45
2

After you closed the file descriptor fd assign -1 to it, so you later could test fd against -1 to see if you already closed it.


You could lookup the filename a (valid) file descriptor referrs to by calling readlink() on /proc/<pid>/fd/<file descriptor>.

alk
  • 69,737
  • 10
  • 105
  • 255