From: Nick Kralevich Date: Fri, 21 Jun 2013 20:28:42 +0000 (-0700) Subject: libc_logging: don't keep file descriptors open forever X-Git-Tag: android-x86-4.4-r1~19^2~63^2^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=17fc25d20f4d61f7fc3dfb3de095719ada89e38b;p=android-x86%2Fbionic.git libc_logging: don't keep file descriptors open forever Avoid keeping unnecessary file descriptors around when they're not needed. Libc doesn't log so much that opening / closing overhead matters. Change-Id: I590ec5c27562db9bac025f781c48ec9a7724ce77 --- diff --git a/libc/bionic/libc_logging.cpp b/libc/bionic/libc_logging.cpp index ffc5335d3..6bf7415d7 100644 --- a/libc/bionic/libc_logging.cpp +++ b/libc/bionic/libc_logging.cpp @@ -42,7 +42,6 @@ #include static pthread_mutex_t gAbortMsgLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t gLogInitializationLock = PTHREAD_MUTEX_INITIALIZER; __LIBC_HIDDEN__ abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common. @@ -421,13 +420,9 @@ int __libc_format_fd(int fd, const char* format, ...) { } static int __libc_write_log(int priority, const char* tag, const char* msg) { - static int main_log_fd = -1; + int main_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/main", O_CLOEXEC | O_WRONLY)); if (main_log_fd == -1) { - ScopedPthreadMutexLocker locker(&gLogInitializationLock); - main_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/main", O_CLOEXEC | O_WRONLY)); - if (main_log_fd == -1) { - return -1; - } + return -1; } iovec vec[3]; @@ -438,7 +433,9 @@ static int __libc_write_log(int priority, const char* tag, const char* msg) { vec[2].iov_base = const_cast(msg); vec[2].iov_len = strlen(msg) + 1; - return TEMP_FAILURE_RETRY(writev(main_log_fd, vec, 3)); + int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, 3)); + close(main_log_fd); + return result; } int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) { @@ -465,12 +462,13 @@ static int __libc_android_log_event(int32_t tag, char type, const void* payload, vec[2].iov_base = const_cast(payload); vec[2].iov_len = len; - static int event_log_fd = -1; + int event_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/events", O_CLOEXEC | O_WRONLY)); if (event_log_fd == -1) { - ScopedPthreadMutexLocker locker(&gLogInitializationLock); - event_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/events", O_CLOEXEC | O_WRONLY)); + return -1; } - return TEMP_FAILURE_RETRY(writev(event_log_fd, vec, 3)); + int result = TEMP_FAILURE_RETRY(writev(event_log_fd, vec, 3)); + close(event_log_fd); + return result; } void __libc_android_log_event_int(int32_t tag, int value) {