Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

c - If close(2) fails with EIO, will the file descriptor still be deleted?

If a close(2) system call fails with EIO, will the file descriptor still be deleted?

If yes, is it not possible to handle a spurious IO error by retrying later? If no, how should one prevent a file descriptor leak?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's a tricky question. However, the POSIX standard does cover it in the description of close():

If close() is interrupted by a signal that is to be caught, it shall return -1 with errno set to [EINTR] and the state of fildes is unspecified. If an I/O error occurred while reading from or writing to the file system during close(), it may return -1 with errno set to [EIO]; if this error is returned, the state of fildes is unspecified.

So, the state of the file descriptor is unspecified by the standard.

For most practical purposes, it is closed; there is precious little you can do with the file descriptor even if it is officially open. You could try an innocuous operation (like fcntl() and F_GETFL) and see whether you get EBADF back, indicating the descriptor is formally closed. But if it is open and the cause of the EIO error is permanent, then you're likely to get EIO every time you try to do anything with it (possibly including the fcntl() call). You might or might not ever get the same descriptor returned by another open-like operation. It is not clear that even dup2() could be successful specifying the 'dead' file descriptor as the target if the dead file descriptor is open but uncloseable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...