Fix flockfile(3) and friends for stdin/stdout/stderr too.
stdin/stdout/stderr are special; their mutexes are initialized by
__sinit. There's no unit test for this, because __sinit has already
been called by the time the first unit test runs, but you could
reproduce this failure with a trivial main() that calls flockfile
or ftrylockfile on one of the standard streams before otherwise
using stdio.
Bug: 18208568
Change-Id: I28d232cf05a9f198a2bed61854d8047b23d2091d
diff --git a/libc/bionic/flockfile.cpp b/libc/bionic/flockfile.cpp
index 3381e8e..b73907cb 100644
--- a/libc/bionic/flockfile.cpp
+++ b/libc/bionic/flockfile.cpp
@@ -36,12 +36,20 @@
// struct __sfileext (see fileext.h).
void flockfile(FILE* fp) {
+ if (!__sdidinit) {
+ __sinit();
+ }
+
if (fp != NULL) {
pthread_mutex_lock(&_FLOCK(fp));
}
}
int ftrylockfile(FILE* fp) {
+ if (!__sdidinit) {
+ __sinit();
+ }
+
// The specification for ftrylockfile() says it returns 0 on success,
// or non-zero on error. So return an errno code directly on error.
if (fp == NULL) {
@@ -52,6 +60,10 @@
}
void funlockfile(FILE* fp) {
+ if (!__sdidinit) {
+ __sinit();
+ }
+
if (fp != NULL) {
pthread_mutex_unlock(&_FLOCK(fp));
}