2009-03-04 04:32:55 +01:00
|
|
|
/*
|
2016-03-01 22:45:42 +01:00
|
|
|
* Copyright (C) 2007-2016 The Android Open Source Project
|
2009-03-04 04:32:55 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2014-02-20 23:59:07 +01:00
|
|
|
#include <errno.h>
|
2015-02-13 00:14:26 +01:00
|
|
|
#include <stdatomic.h>
|
2014-02-20 23:59:07 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-03-01 22:45:42 +01:00
|
|
|
#include <sys/time.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-08-19 02:29:34 +02:00
|
|
|
#ifdef __BIONIC__
|
|
|
|
#include <android/set_abort_message.h>
|
|
|
|
#endif
|
|
|
|
|
2015-03-13 14:50:32 +01:00
|
|
|
#include <log/event_tag_map.h>
|
2017-03-09 16:28:29 +01:00
|
|
|
#include <log/log_transport.h>
|
2014-02-20 23:59:07 +01:00
|
|
|
#include <private/android_filesystem_config.h>
|
2015-01-16 22:38:07 +01:00
|
|
|
#include <private/android_logger.h>
|
2014-01-02 22:52:29 +01:00
|
|
|
|
2016-03-09 01:18:26 +01:00
|
|
|
#include "config_read.h" /* __android_log_config_read_close() definition */
|
2016-03-01 22:45:42 +01:00
|
|
|
#include "config_write.h"
|
|
|
|
#include "log_portability.h"
|
|
|
|
#include "logger.h"
|
2016-03-10 17:25:33 +01:00
|
|
|
|
2013-11-22 16:54:30 +01:00
|
|
|
#define LOG_BUF_SIZE 1024
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
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;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is used by the C++ code to decide if it should write logs through
|
2014-02-20 23:59:07 +01:00
|
|
|
* the C code. Basically, if /dev/socket/logd is available, we're running in
|
2009-03-04 04:32:55 +01:00
|
|
|
* the simulator rather than a desktop tool and want to use the device.
|
|
|
|
*/
|
|
|
|
static enum {
|
2017-03-09 17:09:43 +01:00
|
|
|
kLogUninitialized,
|
|
|
|
kLogNotAvailable,
|
|
|
|
kLogAvailable
|
2009-03-04 04:32:55 +01:00
|
|
|
} g_log_status = kLogUninitialized;
|
2015-02-04 21:28:47 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int check_log_uid_permissions() {
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2017-03-09 17:09:43 +01:00
|
|
|
uid_t uid = __android_log_uid();
|
2016-03-29 01:20:29 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* Matches clientHasLogCredentials() in logd */
|
|
|
|
if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
|
|
|
|
uid = geteuid();
|
2016-03-29 01:20:29 +02:00
|
|
|
if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
|
2017-03-09 17:09:43 +01:00
|
|
|
gid_t gid = getgid();
|
|
|
|
if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
|
|
|
|
gid = getegid();
|
|
|
|
if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
|
|
|
|
int num_groups;
|
|
|
|
gid_t* groups;
|
|
|
|
|
|
|
|
num_groups = getgroups(0, NULL);
|
|
|
|
if (num_groups <= 0) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
groups = calloc(num_groups, sizeof(gid_t));
|
|
|
|
if (!groups) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
num_groups = getgroups(num_groups, groups);
|
|
|
|
while (num_groups > 0) {
|
|
|
|
if (groups[num_groups - 1] == AID_LOG) {
|
|
|
|
break;
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
--num_groups;
|
|
|
|
}
|
|
|
|
free(groups);
|
|
|
|
if (num_groups <= 0) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-29 01:20:29 +02:00
|
|
|
#endif
|
2017-03-09 17:09:43 +01:00
|
|
|
return 0;
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __android_log_cache_available(
|
2017-03-09 17:09:43 +01:00
|
|
|
struct android_log_transport_write* node) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (node->logMask) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
|
|
|
|
if (node->write && (i != LOG_ID_KERNEL) &&
|
|
|
|
((i != LOG_ID_SECURITY) || (check_log_uid_permissions() == 0)) &&
|
|
|
|
(!node->available || ((*node->available)(i) >= 0))) {
|
|
|
|
node->logMask |= 1 << i;
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_dev_available() {
|
|
|
|
struct android_log_transport_write* node;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (list_empty(&__android_log_transport_write)) {
|
|
|
|
return kLogUninitialized;
|
|
|
|
}
|
2016-03-29 01:20:29 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_transport_for_each(node, &__android_log_transport_write) {
|
|
|
|
__android_log_cache_available(node);
|
|
|
|
if (node->logMask) {
|
|
|
|
return kLogAvailable;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
return kLogNotAvailable;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-09-13 16:28:21 +02:00
|
|
|
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-09-13 16:28:21 +02:00
|
|
|
static atomic_uintptr_t tagMap;
|
|
|
|
#endif
|
|
|
|
|
2016-08-23 19:23:36 +02:00
|
|
|
/*
|
|
|
|
* Release any logger resources. A new log write will immediately re-acquire.
|
|
|
|
*/
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC void __android_log_close() {
|
|
|
|
struct android_log_transport_write* transport;
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2017-03-09 17:09:43 +01:00
|
|
|
EventTagMap* m;
|
2016-09-13 16:28:21 +02:00
|
|
|
#endif
|
2016-08-23 19:23:36 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_lock();
|
2016-08-23 19:23:36 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_to_log = __write_to_log_init;
|
2016-08-23 19:23:36 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-08-23 19:23:36 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_transport_for_each(transport, &__android_log_persist_write) {
|
|
|
|
if (transport->close) {
|
|
|
|
(*transport->close)();
|
2016-08-23 19:23:36 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-08-23 19:23:36 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_transport_for_each(transport, &__android_log_transport_write) {
|
|
|
|
if (transport->close) {
|
|
|
|
(*transport->close)();
|
2016-08-23 19:23:36 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-08-23 19:23:36 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_config_write_close();
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2017-03-09 17:09:43 +01:00
|
|
|
/*
|
|
|
|
* Additional risk here somewhat mitigated by immediately unlock flushing
|
|
|
|
* the processor cache. The multi-threaded race that we choose to accept,
|
|
|
|
* to minimize locking, is an atomic_load in a writer picking up a value
|
|
|
|
* just prior to entering this routine. There will be an use after free.
|
|
|
|
*
|
|
|
|
* Again, anyone calling this is doing so to release the logging resources
|
|
|
|
* is most probably going to quiesce then shut down; or to restart after
|
|
|
|
* a fork so the risk should be non-existent. For this reason we
|
|
|
|
* choose a mitigation stance for efficiency instead of incuring the cost
|
|
|
|
* of a lock for every log write.
|
|
|
|
*/
|
|
|
|
m = (EventTagMap*)atomic_exchange(&tagMap, (uintptr_t)0);
|
2016-09-13 16:28:21 +02:00
|
|
|
#endif
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_unlock();
|
2016-09-13 16:28:21 +02:00
|
|
|
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2017-03-09 17:09:43 +01:00
|
|
|
if (m != (EventTagMap*)(uintptr_t)-1LL) android_closeEventTagMap(m);
|
2016-09-13 16:28:21 +02:00
|
|
|
#endif
|
2016-08-23 19:23:36 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2014-03-25 21:45:33 +01:00
|
|
|
/* log_init_lock assumed */
|
2017-03-09 17:09:43 +01:00
|
|
|
static int __write_to_log_initialize() {
|
|
|
|
struct android_log_transport_write* transport;
|
|
|
|
struct listnode* n;
|
|
|
|
int i = 0, ret = 0;
|
|
|
|
|
|
|
|
__android_log_config_write();
|
|
|
|
write_transport_for_each_safe(transport, n, &__android_log_transport_write) {
|
|
|
|
__android_log_cache_available(transport);
|
|
|
|
if (!transport->logMask) {
|
|
|
|
list_remove(&transport->node);
|
|
|
|
continue;
|
2014-03-25 21:45:33 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!transport->open || ((*transport->open)() < 0)) {
|
|
|
|
if (transport->close) {
|
|
|
|
(*transport->close)();
|
|
|
|
}
|
|
|
|
list_remove(&transport->node);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
write_transport_for_each_safe(transport, n, &__android_log_persist_write) {
|
|
|
|
__android_log_cache_available(transport);
|
|
|
|
if (!transport->logMask) {
|
|
|
|
list_remove(&transport->node);
|
|
|
|
continue;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!transport->open || ((*transport->open)() < 0)) {
|
|
|
|
if (transport->close) {
|
|
|
|
(*transport->close)();
|
|
|
|
}
|
|
|
|
list_remove(&transport->node);
|
|
|
|
continue;
|
2014-03-25 21:45:33 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (!ret && !i) {
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2014-03-25 21:45:33 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2014-03-25 21:45:33 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 22:45:42 +01:00
|
|
|
/*
|
|
|
|
* Extract a 4-byte value from a byte stream. le32toh open coded
|
|
|
|
*/
|
2017-03-09 17:09:43 +01:00
|
|
|
static inline uint32_t get4LE(const uint8_t* src) {
|
|
|
|
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int __write_to_log_daemon(log_id_t log_id, struct iovec* vec, size_t nr) {
|
|
|
|
struct android_log_transport_write* node;
|
|
|
|
int ret;
|
|
|
|
struct timespec ts;
|
|
|
|
size_t len, i;
|
2014-04-24 18:43:23 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
for (len = i = 0; i < nr; ++i) {
|
|
|
|
len += vec[i].iov_len;
|
|
|
|
}
|
|
|
|
if (!len) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-03-13 23:57:11 +01:00
|
|
|
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2017-03-09 17:09:43 +01:00
|
|
|
clock_gettime(android_log_clockid(), &ts);
|
2016-12-28 19:30:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (log_id == LOG_ID_SECURITY) {
|
|
|
|
if (vec[0].iov_len < 4) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
ret = check_log_uid_permissions();
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (!__android_log_security()) {
|
|
|
|
/* If only we could reset downstream logd counter */
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
} else if (log_id == LOG_ID_EVENTS) {
|
|
|
|
const char* tag;
|
|
|
|
size_t len;
|
|
|
|
EventTagMap *m, *f;
|
2016-01-06 22:17:43 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (vec[0].iov_len < 4) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-03-13 14:50:32 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
tag = NULL;
|
|
|
|
len = 0;
|
|
|
|
f = NULL;
|
|
|
|
m = (EventTagMap*)atomic_load(&tagMap);
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
ret = __android_log_trylock();
|
|
|
|
m = (EventTagMap*)atomic_load(&tagMap); /* trylock flush cache */
|
|
|
|
if (!m) {
|
|
|
|
m = android_openEventTagMap(NULL);
|
|
|
|
if (ret) { /* trylock failed, use local copy, mark for close */
|
|
|
|
f = m;
|
|
|
|
} else {
|
|
|
|
if (!m) { /* One chance to open map file */
|
|
|
|
m = (EventTagMap*)(uintptr_t)-1LL;
|
|
|
|
}
|
|
|
|
atomic_store(&tagMap, (uintptr_t)m);
|
2016-01-06 22:17:43 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
if (!ret) { /* trylock succeeded, unlock */
|
|
|
|
__android_log_unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m && (m != (EventTagMap*)(uintptr_t)-1LL)) {
|
|
|
|
tag = android_lookupEventTag_len(m, &len, get4LE(vec[0].iov_base));
|
|
|
|
}
|
|
|
|
ret = __android_log_is_loggable_len(ANDROID_LOG_INFO, tag, len,
|
|
|
|
ANDROID_LOG_VERBOSE);
|
|
|
|
if (f) { /* local copy marked for close */
|
|
|
|
android_closeEventTagMap(f);
|
|
|
|
}
|
|
|
|
if (!ret) {
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Validate the incoming tag, tag content can not split across iovec */
|
|
|
|
char prio = ANDROID_LOG_VERBOSE;
|
|
|
|
const char* tag = vec[0].iov_base;
|
|
|
|
size_t len = vec[0].iov_len;
|
|
|
|
if (!tag) {
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
if (len > 0) {
|
|
|
|
prio = *tag;
|
|
|
|
if (len > 1) {
|
|
|
|
--len;
|
|
|
|
++tag;
|
|
|
|
} else {
|
|
|
|
len = vec[1].iov_len;
|
|
|
|
tag = ((const char*)vec[1].iov_base);
|
2015-03-13 14:50:32 +01:00
|
|
|
if (!tag) {
|
2017-03-09 17:09:43 +01:00
|
|
|
len = 0;
|
2015-03-13 14:50:32 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* tag must be nul terminated */
|
|
|
|
if (tag && strnlen(tag, len) >= len) {
|
|
|
|
tag = NULL;
|
|
|
|
}
|
2015-03-13 14:50:32 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!__android_log_is_loggable_len(prio, tag, len - 1, ANDROID_LOG_VERBOSE)) {
|
|
|
|
return -EPERM;
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
#else
|
2017-03-09 17:09:43 +01:00
|
|
|
/* simulate clock_gettime(CLOCK_REALTIME, &ts); */
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
ts.tv_sec = tv.tv_sec;
|
|
|
|
ts.tv_nsec = tv.tv_usec * 1000;
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
#endif
|
2014-02-20 23:59:07 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
ret = 0;
|
|
|
|
i = 1 << log_id;
|
|
|
|
write_transport_for_each(node, &__android_log_transport_write) {
|
|
|
|
if (node->logMask & i) {
|
|
|
|
ssize_t retval;
|
|
|
|
retval = (*node->write)(log_id, &ts, vec, nr);
|
|
|
|
if (ret >= 0) {
|
|
|
|
ret = retval;
|
|
|
|
}
|
2014-03-25 21:45:33 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2014-04-24 18:43:23 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_transport_for_each(node, &__android_log_persist_write) {
|
|
|
|
if (node->logMask & i) {
|
|
|
|
(void)(*node->write)(log_id, &ts, vec, nr);
|
2014-04-24 18:43:23 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2014-04-24 18:43:23 +02:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2014-02-20 23:59:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int __write_to_log_init(log_id_t log_id, struct iovec* vec, size_t nr) {
|
|
|
|
__android_log_lock();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (write_to_log == __write_to_log_init) {
|
|
|
|
int ret;
|
2014-03-25 21:45:33 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
ret = __write_to_log_initialize();
|
|
|
|
if (ret < 0) {
|
|
|
|
__android_log_unlock();
|
|
|
|
if (!list_empty(&__android_log_persist_write)) {
|
|
|
|
__write_to_log_daemon(log_id, vec, nr);
|
|
|
|
}
|
|
|
|
return ret;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_to_log = __write_to_log_daemon;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_unlock();
|
|
|
|
|
|
|
|
return write_to_log(log_id, vec, nr);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_write(int prio, const char* tag,
|
|
|
|
const char* msg) {
|
|
|
|
return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 17:25:33 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_buf_write(int bufID, int prio,
|
2017-03-09 17:09:43 +01:00
|
|
|
const char* tag, const char* msg) {
|
|
|
|
struct iovec vec[3];
|
|
|
|
char tmp_tag[32];
|
|
|
|
|
|
|
|
if (!tag) tag = "";
|
|
|
|
|
|
|
|
/* XXX: This needs to go! */
|
2017-03-28 18:49:53 +02:00
|
|
|
if (bufID != LOG_ID_RADIO) {
|
|
|
|
switch (tag[0]) {
|
|
|
|
case 'H':
|
|
|
|
if (strcmp(tag + 1, "HTC_RIL" + 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'R':
|
|
|
|
/* Any log tag with "RIL" as the prefix */
|
|
|
|
if (strncmp(tag + 1, "RIL" + 1, strlen("RIL") - 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'Q':
|
|
|
|
/* Any log tag with "QC_RIL" as the prefix */
|
|
|
|
if (strncmp(tag + 1, "QC_RIL" + 1, strlen("QC_RIL") - 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'I':
|
|
|
|
/* Any log tag with "IMS" as the prefix */
|
|
|
|
if (strncmp(tag + 1, "IMS" + 1, strlen("IMS") - 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'A':
|
|
|
|
if (strcmp(tag + 1, "AT" + 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'G':
|
|
|
|
if (strcmp(tag + 1, "GSM" + 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'S':
|
|
|
|
if (strcmp(tag + 1, "STK" + 1) && strcmp(tag + 1, "SMS" + 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'C':
|
|
|
|
if (strcmp(tag + 1, "CDMA" + 1)) break;
|
|
|
|
goto inform;
|
|
|
|
case 'P':
|
|
|
|
if (strcmp(tag + 1, "PHONE" + 1)) break;
|
|
|
|
/* FALLTHRU */
|
|
|
|
inform:
|
|
|
|
bufID = LOG_ID_RADIO;
|
|
|
|
snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
|
|
|
|
tag = tmp_tag;
|
|
|
|
/* FALLTHRU */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2010-03-01 18:11:54 +01:00
|
|
|
|
2015-04-02 19:32:44 +02:00
|
|
|
#if __BIONIC__
|
2017-03-09 17:09:43 +01:00
|
|
|
if (prio == ANDROID_LOG_FATAL) {
|
|
|
|
android_set_abort_message(msg);
|
|
|
|
}
|
2015-04-02 19:32:44 +02:00
|
|
|
#endif
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
vec[0].iov_base = (unsigned char*)&prio;
|
|
|
|
vec[0].iov_len = 1;
|
|
|
|
vec[1].iov_base = (void*)tag;
|
|
|
|
vec[1].iov_len = strlen(tag) + 1;
|
|
|
|
vec[2].iov_base = (void*)msg;
|
|
|
|
vec[2].iov_len = strlen(msg) + 1;
|
2010-03-01 18:11:54 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return write_to_log(bufID, vec, 3);
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_vprint(int prio, const char* tag,
|
|
|
|
const char* fmt, va_list ap) {
|
|
|
|
char buf[LOG_BUF_SIZE];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return __android_log_write(prio, tag, buf);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_print(int prio, const char* tag,
|
|
|
|
const char* fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char buf[LOG_BUF_SIZE];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
|
|
|
|
va_end(ap);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return __android_log_write(prio, tag, buf);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 17:25:33 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_buf_print(int bufID, int prio,
|
2017-03-09 17:09:43 +01:00
|
|
|
const char* tag, const char* fmt,
|
|
|
|
...) {
|
|
|
|
va_list ap;
|
|
|
|
char buf[LOG_BUF_SIZE];
|
2010-03-01 18:11:54 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
|
|
|
|
va_end(ap);
|
2010-03-01 18:11:54 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return __android_log_buf_write(bufID, prio, tag, buf);
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC void __android_log_assert(const char* cond, const char* tag,
|
|
|
|
const char* fmt, ...) {
|
|
|
|
char buf[LOG_BUF_SIZE];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (fmt) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
} else {
|
|
|
|
/* Msg not provided, log condition. N.B. Do not use cond directly as
|
|
|
|
* format string as it could contain spurious '%' syntax (e.g.
|
|
|
|
* "%d" in "blocks%devs == 0").
|
|
|
|
*/
|
|
|
|
if (cond)
|
|
|
|
snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
|
|
|
|
else
|
|
|
|
strcpy(buf, "Unspecified assertion failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log assertion failures to stderr for the benefit of "adb shell" users
|
|
|
|
// and gtests (http://b/23675822).
|
|
|
|
struct iovec iov[2] = {
|
|
|
|
{ buf, strlen(buf) }, { (char*)"\n", 1 },
|
|
|
|
};
|
|
|
|
TEMP_FAILURE_RETRY(writev(2, iov, 2));
|
|
|
|
|
|
|
|
__android_log_write(ANDROID_LOG_FATAL, tag, buf);
|
|
|
|
abort(); /* abort so we have a chance to debug the situation */
|
|
|
|
/* NOTREACHED */
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_bwrite(int32_t tag, const void* payload,
|
|
|
|
size_t len) {
|
|
|
|
struct iovec vec[2];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
vec[0].iov_base = &tag;
|
|
|
|
vec[0].iov_len = sizeof(tag);
|
|
|
|
vec[1].iov_base = (void*)payload;
|
|
|
|
vec[1].iov_len = len;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return write_to_log(LOG_ID_EVENTS, vec, 2);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 17:25:33 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_security_bwrite(int32_t tag,
|
2017-03-09 17:09:43 +01:00
|
|
|
const void* payload,
|
|
|
|
size_t len) {
|
|
|
|
struct iovec vec[2];
|
2015-12-04 19:59:45 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
vec[0].iov_base = &tag;
|
|
|
|
vec[0].iov_len = sizeof(tag);
|
|
|
|
vec[1].iov_base = (void*)payload;
|
|
|
|
vec[1].iov_len = len;
|
2015-12-04 19:59:45 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return write_to_log(LOG_ID_SECURITY, vec, 2);
|
2015-12-04 19:59:45 +01:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Like __android_log_bwrite, but takes the type as well. Doesn't work
|
|
|
|
* for the general case where we're generating lists of stuff, but very
|
|
|
|
* handy if we just want to dump an integer into the log.
|
|
|
|
*/
|
2016-03-10 17:25:33 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_btwrite(int32_t tag, char type,
|
2017-03-09 17:09:43 +01:00
|
|
|
const void* payload, size_t len) {
|
|
|
|
struct iovec vec[3];
|
|
|
|
|
|
|
|
vec[0].iov_base = &tag;
|
|
|
|
vec[0].iov_len = sizeof(tag);
|
|
|
|
vec[1].iov_base = &type;
|
|
|
|
vec[1].iov_len = sizeof(type);
|
|
|
|
vec[2].iov_base = (void*)payload;
|
|
|
|
vec[2].iov_len = len;
|
|
|
|
|
|
|
|
return write_to_log(LOG_ID_EVENTS, vec, 3);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2014-07-01 19:57:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like __android_log_bwrite, but used for writing strings to the
|
|
|
|
* event log.
|
|
|
|
*/
|
2017-03-09 17:09:43 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_bswrite(int32_t tag, const char* payload) {
|
|
|
|
struct iovec vec[4];
|
|
|
|
char type = EVENT_TYPE_STRING;
|
|
|
|
uint32_t len = strlen(payload);
|
|
|
|
|
|
|
|
vec[0].iov_base = &tag;
|
|
|
|
vec[0].iov_len = sizeof(tag);
|
|
|
|
vec[1].iov_base = &type;
|
|
|
|
vec[1].iov_len = sizeof(type);
|
|
|
|
vec[2].iov_base = &len;
|
|
|
|
vec[2].iov_len = sizeof(len);
|
|
|
|
vec[3].iov_base = (void*)payload;
|
|
|
|
vec[3].iov_len = len;
|
|
|
|
|
|
|
|
return write_to_log(LOG_ID_EVENTS, vec, 4);
|
2014-07-01 19:57:16 +02:00
|
|
|
}
|
2016-01-10 23:31:19 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like __android_log_security_bwrite, but used for writing strings to the
|
|
|
|
* security log.
|
|
|
|
*/
|
2016-03-10 17:25:33 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int __android_log_security_bswrite(int32_t tag,
|
2017-03-09 17:09:43 +01:00
|
|
|
const char* payload) {
|
|
|
|
struct iovec vec[4];
|
|
|
|
char type = EVENT_TYPE_STRING;
|
|
|
|
uint32_t len = strlen(payload);
|
|
|
|
|
|
|
|
vec[0].iov_base = &tag;
|
|
|
|
vec[0].iov_len = sizeof(tag);
|
|
|
|
vec[1].iov_base = &type;
|
|
|
|
vec[1].iov_len = sizeof(type);
|
|
|
|
vec[2].iov_base = &len;
|
|
|
|
vec[2].iov_len = sizeof(len);
|
|
|
|
vec[3].iov_base = (void*)payload;
|
|
|
|
vec[3].iov_len = len;
|
|
|
|
|
|
|
|
return write_to_log(LOG_ID_SECURITY, vec, 4);
|
2016-01-10 23:31:19 +01:00
|
|
|
}
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int __write_to_log_null(log_id_t log_id, struct iovec* vec, size_t nr) {
|
|
|
|
size_t len, i;
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if ((log_id < LOG_ID_MIN) || (log_id >= LOG_ID_MAX)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
for (len = i = 0; i < nr; ++i) {
|
|
|
|
len += vec[i].iov_len;
|
|
|
|
}
|
|
|
|
if (!len) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return len;
|
2015-02-04 21:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Following functions need access to our internal write_to_log status */
|
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
LIBLOG_HIDDEN int __android_log_transport;
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int android_set_log_transport(int transport_flag) {
|
2017-03-09 17:09:43 +01:00
|
|
|
int retval;
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
if (transport_flag < 0) {
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
retval = LOGGER_NULL;
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_lock();
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
if (transport_flag & LOGGER_NULL) {
|
2017-03-09 17:09:43 +01:00
|
|
|
write_to_log = __write_to_log_null;
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_unlock();
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return retval;
|
|
|
|
}
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
__android_log_transport &= LOGGER_LOCAL | LOGGER_LOGD | LOGGER_STDERR;
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
transport_flag &= LOGGER_LOCAL | LOGGER_LOGD | LOGGER_STDERR;
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
if (__android_log_transport != transport_flag) {
|
|
|
|
__android_log_transport = transport_flag;
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_config_write_close();
|
|
|
|
__android_log_config_read_close();
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
write_to_log = __write_to_log_init;
|
2015-02-04 21:39:57 +01:00
|
|
|
/* generically we only expect these two values for write_to_log */
|
2017-03-09 17:09:43 +01:00
|
|
|
} else if ((write_to_log != __write_to_log_init) &&
|
|
|
|
(write_to_log != __write_to_log_daemon)) {
|
|
|
|
write_to_log = __write_to_log_init;
|
|
|
|
}
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
retval = __android_log_transport;
|
2016-03-09 01:18:26 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
__android_log_unlock();
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return retval;
|
2015-02-04 21:39:57 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 16:28:29 +01:00
|
|
|
LIBLOG_ABI_PUBLIC int android_get_log_transport() {
|
2017-03-09 17:09:43 +01:00
|
|
|
int ret = LOGGER_DEFAULT;
|
|
|
|
|
|
|
|
__android_log_lock();
|
|
|
|
if (write_to_log == __write_to_log_null) {
|
|
|
|
ret = LOGGER_NULL;
|
|
|
|
} else {
|
2017-03-09 16:28:29 +01:00
|
|
|
__android_log_transport &= LOGGER_LOCAL | LOGGER_LOGD | LOGGER_STDERR;
|
|
|
|
ret = __android_log_transport;
|
2017-03-09 17:09:43 +01:00
|
|
|
if ((write_to_log != __write_to_log_init) &&
|
|
|
|
(write_to_log != __write_to_log_daemon)) {
|
|
|
|
ret = -EINVAL;
|
2015-02-04 21:39:57 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
__android_log_unlock();
|
2015-02-04 21:39:57 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2015-02-04 21:39:57 +01:00
|
|
|
}
|