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>
|
|
|
|
#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
|
|
|
|
|
2020-01-14 18:52:10 +01:00
|
|
|
#include <android-base/macros.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-01 22:45:42 +01:00
|
|
|
#include "logger.h"
|
2019-01-16 23:17:08 +01:00
|
|
|
#include "uio.h"
|
2016-03-10 17:25:33 +01:00
|
|
|
|
2019-10-02 19:52:55 +02:00
|
|
|
#if (FAKE_LOG_DEVICE == 0)
|
2020-01-09 00:18:26 +01:00
|
|
|
#include "logd_writer.h"
|
|
|
|
#include "pmsg_writer.h"
|
2019-10-02 19:52:55 +02:00
|
|
|
#else
|
2020-01-09 00:18:26 +01:00
|
|
|
#include "fake_log_device.h"
|
2019-10-02 19:52:55 +02:00
|
|
|
#endif
|
2019-10-01 22:05:58 +02:00
|
|
|
|
2020-01-09 00:18:26 +01:00
|
|
|
#define LOG_BUF_SIZE 1024
|
|
|
|
|
2016-11-29 22:39:55 +01:00
|
|
|
#if defined(__ANDROID__)
|
2019-12-11 23:26:37 +01:00
|
|
|
static int check_log_uid_permissions() {
|
2020-01-09 00:34:14 +01:00
|
|
|
uid_t uid = getuid();
|
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;
|
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
groups = static_cast<gid_t*>(calloc(num_groups, sizeof(gid_t)));
|
2017-03-09 17:09:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
return 0;
|
2016-03-29 01:20:29 +02:00
|
|
|
}
|
2019-12-11 23:26:37 +01:00
|
|
|
#endif
|
2016-03-29 01:20:29 +02:00
|
|
|
|
2016-08-23 19:23:36 +02:00
|
|
|
/*
|
|
|
|
* Release any logger resources. A new log write will immediately re-acquire.
|
|
|
|
*/
|
2019-02-08 20:46:19 +01:00
|
|
|
void __android_log_close() {
|
2020-01-09 00:18:26 +01:00
|
|
|
#if (FAKE_LOG_DEVICE == 0)
|
|
|
|
LogdClose();
|
|
|
|
PmsgClose();
|
|
|
|
#else
|
|
|
|
FakeClose();
|
|
|
|
#endif
|
2019-10-01 22:05:58 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 23:26:37 +01:00
|
|
|
static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
|
2018-03-07 19:42:06 +01:00
|
|
|
int ret, save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
struct timespec ts;
|
2015-03-13 23:57:11 +01:00
|
|
|
|
2018-03-07 19:42:06 +01:00
|
|
|
save_errno = errno;
|
2019-12-11 23:26:37 +01:00
|
|
|
|
|
|
|
if (log_id == LOG_ID_KERNEL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-03-07 19:42:06 +01:00
|
|
|
errno = save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
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) {
|
2018-03-07 19:42:06 +01:00
|
|
|
errno = save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (!__android_log_security()) {
|
|
|
|
/* If only we could reset downstream logd counter */
|
2018-03-07 19:42:06 +01:00
|
|
|
errno = save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EPERM;
|
|
|
|
}
|
2017-12-02 00:48:19 +01:00
|
|
|
} else if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
|
2017-03-09 17:09:43 +01:00
|
|
|
if (vec[0].iov_len < 4) {
|
2018-03-07 19:42:06 +01:00
|
|
|
errno = save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-02 02:13:43 +02:00
|
|
|
int prio = *static_cast<int*>(vec[0].iov_base);
|
|
|
|
const char* tag = static_cast<const char*>(vec[1].iov_base);
|
|
|
|
size_t len = vec[1].iov_len;
|
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)) {
|
2018-03-07 19:42:06 +01:00
|
|
|
errno = save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
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;
|
2019-10-01 22:05:58 +02:00
|
|
|
|
2020-01-09 00:18:26 +01:00
|
|
|
#if (FAKE_LOG_DEVICE == 0)
|
|
|
|
ret = LogdWrite(log_id, &ts, vec, nr);
|
|
|
|
PmsgWrite(log_id, &ts, vec, nr);
|
|
|
|
#else
|
|
|
|
ret = FakeWrite(log_id, &ts, vec, nr);
|
|
|
|
#endif
|
2014-04-24 18:43:23 +02:00
|
|
|
|
2018-03-07 19:42:06 +01:00
|
|
|
errno = save_errno;
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2014-02-20 23:59:07 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_write(int prio, const char* tag, const char* msg) {
|
2017-03-09 17:09:43 +01:00
|
|
|
return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_buf_write(int bufID, int prio, const char* tag, const char* msg) {
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!tag) tag = "";
|
|
|
|
|
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
|
|
|
|
|
2015-04-03 22:24:37 +02:00
|
|
|
struct iovec vec[3];
|
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
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
return write_to_log(static_cast<log_id_t>(bufID), vec, 3);
|
2010-03-01 18:11:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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).
|
2019-01-16 23:17:08 +01:00
|
|
|
TEMP_FAILURE_RETRY(write(2, buf, strlen(buf)));
|
|
|
|
TEMP_FAILURE_RETRY(write(2, "\n", 1));
|
2017-03-09 17:09:43 +01:00
|
|
|
|
|
|
|
__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
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_bwrite(int32_t tag, const void* payload, size_t len) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len) {
|
2017-08-25 05:14:06 +02:00
|
|
|
struct iovec vec[2];
|
|
|
|
|
|
|
|
vec[0].iov_base = &tag;
|
|
|
|
vec[0].iov_len = sizeof(tag);
|
|
|
|
vec[1].iov_base = (void*)payload;
|
|
|
|
vec[1].iov_len = len;
|
|
|
|
|
|
|
|
return write_to_log(LOG_ID_STATS, vec, 2);
|
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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.
|
|
|
|
*/
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_btwrite(int32_t tag, char type, const void* payload, size_t len) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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.
|
|
|
|
*/
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_bswrite(int32_t tag, const char* payload) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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.
|
|
|
|
*/
|
2019-02-08 20:46:19 +01:00
|
|
|
int __android_log_security_bswrite(int32_t tag, const char* payload) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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
|
|
|
}
|