liblog: have writers handle their own state
Remove the transport available and open functions since the writers are able to manage their own state. Remove the initialization dance with write_to_log, since it is unneeded once this is removed as well. Remove the global lock around the close() functions as correct locking has been added to the writers in a previous change. Test: logging works, liblog-unit-tests Change-Id: If7fa11e773763d0b5fcb2e696ad1c88ff4a4cfdf
This commit is contained in:
parent
2a6811b4d1
commit
06e0fced63
7 changed files with 10 additions and 175 deletions
|
@ -17,7 +17,6 @@
|
|||
liblog_sources = [
|
||||
"log_event_list.cpp",
|
||||
"log_event_write.cpp",
|
||||
"logger_lock.cpp",
|
||||
"logger_name.cpp",
|
||||
"logger_read.cpp",
|
||||
"logger_write.cpp",
|
||||
|
|
|
@ -52,10 +52,6 @@ static void FakeClose();
|
|||
static int FakeWrite(log_id_t log_id, struct timespec* ts, struct iovec* vec, size_t nr);
|
||||
|
||||
struct android_log_transport_write fakeLoggerWrite = {
|
||||
.name = "fake",
|
||||
.logMask = 0,
|
||||
.available = [](log_id_t) { return 0; },
|
||||
.open = [] { return 0; },
|
||||
.close = FakeClose,
|
||||
.write = FakeWrite,
|
||||
};
|
||||
|
|
|
@ -45,10 +45,6 @@ static int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz
|
|||
static void LogdClose();
|
||||
|
||||
struct android_log_transport_write logdLoggerWrite = {
|
||||
.name = "logd",
|
||||
.logMask = 0,
|
||||
.available = [](log_id_t) { return 0; },
|
||||
.open = [] { return 0; },
|
||||
.close = LogdClose,
|
||||
.write = LogdWrite,
|
||||
};
|
||||
|
|
|
@ -26,20 +26,7 @@
|
|||
|
||||
__BEGIN_DECLS
|
||||
|
||||
/* Union, sock or fd of zero is not allowed unless static initialized */
|
||||
union android_log_context_union {
|
||||
void* priv;
|
||||
atomic_int sock;
|
||||
atomic_int fd;
|
||||
};
|
||||
|
||||
struct android_log_transport_write {
|
||||
const char* name; /* human name to describe the transport */
|
||||
unsigned logMask; /* mask cache of available() success */
|
||||
union android_log_context_union context; /* Initialized by static allocation */
|
||||
|
||||
int (*available)(log_id_t logId); /* Does not cause resources to be taken */
|
||||
int (*open)(); /* can be called multiple times, reusing current resources */
|
||||
void (*close)(); /* free up resources */
|
||||
/* write log to transport, returns number of bytes propagated, or -errno */
|
||||
int (*write)(log_id_t logId, struct timespec* ts, struct iovec* vec,
|
||||
|
@ -83,8 +70,4 @@ static inline uid_t __android_log_uid() {
|
|||
}
|
||||
#endif
|
||||
|
||||
void __android_log_lock();
|
||||
int __android_log_trylock();
|
||||
void __android_log_unlock();
|
||||
|
||||
__END_DECLS
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2007-2016 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Some OS specific dribs and drabs (locking etc).
|
||||
*/
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
#if !defined(_WIN32)
|
||||
static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
|
||||
void __android_log_lock() {
|
||||
#if !defined(_WIN32)
|
||||
/*
|
||||
* If we trigger a signal handler in the middle of locked activity and the
|
||||
* signal handler logs a message, we could get into a deadlock state.
|
||||
*/
|
||||
pthread_mutex_lock(&log_init_lock);
|
||||
#endif
|
||||
}
|
||||
|
||||
int __android_log_trylock() {
|
||||
#if !defined(_WIN32)
|
||||
return pthread_mutex_trylock(&log_init_lock);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void __android_log_unlock() {
|
||||
#if !defined(_WIN32)
|
||||
pthread_mutex_unlock(&log_init_lock);
|
||||
#endif
|
||||
}
|
|
@ -46,11 +46,8 @@ android_log_transport_write* android_log_write = &fakeLoggerWrite;
|
|||
android_log_transport_write* android_log_persist_write = nullptr;
|
||||
#endif
|
||||
|
||||
static int __write_to_log_init(log_id_t, struct iovec* vec, size_t nr);
|
||||
static int (*write_to_log)(log_id_t, struct iovec* vec, size_t nr) = __write_to_log_init;
|
||||
|
||||
static int check_log_uid_permissions() {
|
||||
#if defined(__ANDROID__)
|
||||
static int check_log_uid_permissions() {
|
||||
uid_t uid = __android_log_uid();
|
||||
|
||||
/* Matches clientHasLogCredentials() in logd */
|
||||
|
@ -87,43 +84,14 @@ static int check_log_uid_permissions() {
|
|||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __android_log_cache_available(struct android_log_transport_write* node) {
|
||||
uint32_t i;
|
||||
|
||||
if (node->logMask) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
|
||||
if (i != LOG_ID_KERNEL && (i != LOG_ID_SECURITY || check_log_uid_permissions() == 0) &&
|
||||
(*node->available)(static_cast<log_id_t>(i)) >= 0) {
|
||||
node->logMask |= 1 << i;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Release any logger resources. A new log write will immediately re-acquire.
|
||||
*/
|
||||
void __android_log_close() {
|
||||
__android_log_lock();
|
||||
|
||||
write_to_log = __write_to_log_init;
|
||||
|
||||
/*
|
||||
* Threads that are actively writing at this point are not held back
|
||||
* by a lock and are at risk of dropping the messages with a return code
|
||||
* -EBADF. Prefer to return error code than add the overhead of a lock to
|
||||
* each log writing call to guarantee delivery. In addition, anyone
|
||||
* calling this is doing so to release the logging resources and shut down,
|
||||
* for them to do so with outstanding log requests in other threads is a
|
||||
* disengenuous use of this function.
|
||||
*/
|
||||
|
||||
if (android_log_write != nullptr) {
|
||||
android_log_write->close();
|
||||
}
|
||||
|
@ -132,44 +100,18 @@ void __android_log_close() {
|
|||
android_log_persist_write->close();
|
||||
}
|
||||
|
||||
__android_log_unlock();
|
||||
}
|
||||
|
||||
static bool transport_initialize(android_log_transport_write* transport) {
|
||||
if (transport == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
__android_log_cache_available(transport);
|
||||
if (!transport->logMask) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Do we actually need to call close() if open() fails?
|
||||
if (transport->open() < 0) {
|
||||
transport->close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* log_init_lock assumed */
|
||||
static int __write_to_log_initialize() {
|
||||
if (!transport_initialize(android_log_write)) {
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
transport_initialize(android_log_persist_write);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr) {
|
||||
static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
|
||||
int ret, save_errno;
|
||||
struct timespec ts;
|
||||
|
||||
save_errno = errno;
|
||||
|
||||
if (log_id == LOG_ID_KERNEL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
clock_gettime(android_log_clockid(), &ts);
|
||||
|
||||
|
@ -215,9 +157,8 @@ static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr)
|
|||
#endif
|
||||
|
||||
ret = 0;
|
||||
size_t i = 1 << log_id;
|
||||
|
||||
if (android_log_write != nullptr && (android_log_write->logMask & i)) {
|
||||
if (android_log_write != nullptr) {
|
||||
ssize_t retval;
|
||||
retval = android_log_write->write(log_id, &ts, vec, nr);
|
||||
if (ret >= 0) {
|
||||
|
@ -225,7 +166,7 @@ static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr)
|
|||
}
|
||||
}
|
||||
|
||||
if (android_log_persist_write != nullptr && (android_log_persist_write->logMask & i)) {
|
||||
if (android_log_persist_write != nullptr) {
|
||||
android_log_persist_write->write(log_id, &ts, vec, nr);
|
||||
}
|
||||
|
||||
|
@ -233,29 +174,6 @@ static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int __write_to_log_init(log_id_t log_id, struct iovec* vec, size_t nr) {
|
||||
int ret, save_errno = errno;
|
||||
|
||||
__android_log_lock();
|
||||
|
||||
if (write_to_log == __write_to_log_init) {
|
||||
ret = __write_to_log_initialize();
|
||||
if (ret < 0) {
|
||||
__android_log_unlock();
|
||||
errno = save_errno;
|
||||
return ret;
|
||||
}
|
||||
|
||||
write_to_log = __write_to_log_daemon;
|
||||
}
|
||||
|
||||
__android_log_unlock();
|
||||
|
||||
ret = write_to_log(log_id, vec, nr);
|
||||
errno = save_errno;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __android_log_write(int prio, const char* tag, const char* msg) {
|
||||
return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
|
||||
}
|
||||
|
|
|
@ -40,10 +40,6 @@ static void PmsgClose();
|
|||
static int PmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
|
||||
|
||||
struct android_log_transport_write pmsgLoggerWrite = {
|
||||
.name = "pmsg",
|
||||
.logMask = 0,
|
||||
.available = [](log_id_t) { return 0; },
|
||||
.open = [] { return 0; },
|
||||
.close = PmsgClose,
|
||||
.write = PmsgWrite,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue