Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 243
fornwall Avatar asked Sep 04 '11 02:09

fornwall


People also ask

What does it mean to close a file descriptor?

Description. close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).

What does close return in c?

RETURN VALUE Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and errno set to indicate the error.

What does close return?

close() returns zero on success. On error, -1 is returned, and errno is set to indicate the error.


Video Answer


1 Answers

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.

like image 89
Jonathan Leffler Avatar answered Sep 19 '22 19:09

Jonathan Leffler