How to check if a given file descriptor stored in a variable is still valid?

前端 未结 6 1001
孤独总比滥情好
孤独总比滥情好 2020-11-29 01:17

I have a file descriptor stored in a variable say var. How can I check whether that descriptor is valid at a later stage?

  fdvar1= open(.....);
  fdvar2 =          


        
6条回答
  •  温柔的废话
    2020-11-29 01:55

    It seems to me that if you want to know if it still points to the same resource, one (non-perfect) approach would be to fstat() the descriptor just after open and then later you can do it again and compare the results. Start by looking at .st_mode& S_IFMT and go from there -- is it a filesystem object? Look at .st_dev / .st_ino. Is it a socket? Try getsockname(), getpeername(). It won't be 100% certain, but it can tell you if it definitely isn't the same.

提交回复
热议问题