mirror of
https://github.com/checkpoint-restore/criu.git
synced 2026-01-22 18:05:10 +00:00
util: Make close_safe() reset fd to -1 even on close() failure
The "man 2 close":"Dealing with error returns from close()" says:
"Retrying the close() after a failure return is the wrong thing to do"
We should not leave the fd there, attempting to close it again on next
close()/close_safe() may lead to accidentally closing something else.
It confirms with the kernel code where sys_close() removes fd from
fdtable in this stack:
+-> sys_close
+-> file_close_fd
+-> file_close_fd_locked
+-> rcu_assign_pointer(fdt->fd[fd], NULL)
If there was an fd this stack is always reached and fd is always
removed.
Let's replace the fd with -1 after close no matter what.
Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
This commit is contained in:
parent
d4e8114130
commit
2e5f9facf9
1 changed files with 3 additions and 4 deletions
|
|
@ -222,10 +222,9 @@ int close_safe(int *fd)
|
|||
|
||||
if (*fd > -1) {
|
||||
ret = close(*fd);
|
||||
if (!ret)
|
||||
*fd = -1;
|
||||
else
|
||||
pr_perror("Unable to close fd %d", *fd);
|
||||
if (ret)
|
||||
pr_perror("Failed closing fd %d", *fd);
|
||||
*fd = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue