fclose: avoid double close race when possible
authorEric Blake <eblake@redhat.com>
Tue, 10 May 2011 21:28:48 +0000 (15:28 -0600)
committerEric Blake <eblake@redhat.com>
Tue, 10 May 2011 21:32:44 +0000 (15:32 -0600)
commit112b21e3861a4887731b61888d1388127957ba93
treeb5af7b7828ae575e9fc15f9e7bb44cfa8b675962
parent38ed8cbdaac9ed0f1c8cbedfac78ca3f10991528
fclose: avoid double close race when possible

Calling close(fileno(fp)) prior to fclose(fp) is racy in a
multi-threaded application - some other thread could open a new file,
which is then inadvertently closed by the fclose that we thought
should fail with EBADF.  For mingw, this is no worse than the race
already present in close_fd_maybe_socket for calling closesocket()
prior to _close(), but for all other platforms, we might as well be
nice and avoid the race.

* lib/fclose.c (rpl_fclose): Rewrite to avoid double-close race on
all but WINDOWS_SOCKETS.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
lib/fclose.c