2016-03-01 22:45:42 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pmsg write handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-10-10 16:27:42 +02:00
|
|
|
#include <stdbool.h>
|
2016-03-01 22:45:42 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2017-04-14 08:48:57 +02:00
|
|
|
#include <log/log_properties.h>
|
2016-03-01 22:45:42 +01:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <private/android_logger.h>
|
|
|
|
|
|
|
|
#include "config_write.h"
|
|
|
|
#include "log_portability.h"
|
|
|
|
#include "logger.h"
|
2019-01-16 23:17:08 +01:00
|
|
|
#include "uio.h"
|
2016-03-01 22:45:42 +01:00
|
|
|
|
|
|
|
static int pmsgOpen();
|
|
|
|
static void pmsgClose();
|
|
|
|
static int pmsgAvailable(log_id_t logId);
|
2019-01-10 19:37:36 +01:00
|
|
|
static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
struct android_log_transport_write pmsgLoggerWrite = {
|
2019-01-10 19:37:36 +01:00
|
|
|
.node = {&pmsgLoggerWrite.node, &pmsgLoggerWrite.node},
|
|
|
|
.context.fd = -1,
|
|
|
|
.name = "pmsg",
|
|
|
|
.available = pmsgAvailable,
|
|
|
|
.open = pmsgOpen,
|
|
|
|
.close = pmsgClose,
|
|
|
|
.write = pmsgWrite,
|
2016-03-01 22:45:42 +01:00
|
|
|
};
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int pmsgOpen() {
|
|
|
|
int fd = atomic_load(&pmsgLoggerWrite.context.fd);
|
|
|
|
if (fd < 0) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
|
|
|
|
i = atomic_exchange(&pmsgLoggerWrite.context.fd, fd);
|
|
|
|
if ((i >= 0) && (i != fd)) {
|
|
|
|
close(i);
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return fd;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static void pmsgClose() {
|
|
|
|
int fd = atomic_exchange(&pmsgLoggerWrite.context.fd, -1);
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int pmsgAvailable(log_id_t logId) {
|
|
|
|
if (logId > LOG_ID_SECURITY) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
if ((logId != LOG_ID_SECURITY) && (logId != LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
|
|
|
|
if (access("/dev/pmsg0", W_OK) == 0) {
|
|
|
|
return 0;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
return 1;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 23:50:46 +01:00
|
|
|
/*
|
|
|
|
* Extract a 4-byte value from a byte stream.
|
|
|
|
*/
|
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-25 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
|
2017-03-09 17:09:43 +01:00
|
|
|
static const unsigned headerLength = 2;
|
|
|
|
struct iovec newVec[nr + headerLength];
|
|
|
|
android_log_header_t header;
|
|
|
|
android_pmsg_log_header_t pmsgHeader;
|
|
|
|
size_t i, payloadSize;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if ((logId == LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
|
|
|
|
if (vec[0].iov_len < 4) {
|
|
|
|
return -EINVAL;
|
2016-03-25 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
if (SNET_EVENT_LOG_TAG != get4LE(static_cast<uint8_t*>(vec[0].iov_base))) {
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EPERM;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* // what we provide to pstore
|
|
|
|
* android_pmsg_log_header_t pmsgHeader;
|
|
|
|
* // what we provide to file
|
|
|
|
* android_log_header_t header;
|
|
|
|
* // caller provides
|
|
|
|
* union {
|
|
|
|
* struct {
|
|
|
|
* char prio;
|
|
|
|
* char payload[];
|
|
|
|
* } string;
|
|
|
|
* struct {
|
|
|
|
* uint32_t tag
|
|
|
|
* char payload[];
|
|
|
|
* } binary;
|
|
|
|
* };
|
|
|
|
* };
|
|
|
|
*/
|
|
|
|
|
|
|
|
pmsgHeader.magic = LOGGER_MAGIC;
|
|
|
|
pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header);
|
|
|
|
pmsgHeader.uid = __android_log_uid();
|
|
|
|
pmsgHeader.pid = getpid();
|
|
|
|
|
|
|
|
header.id = logId;
|
|
|
|
header.tid = gettid();
|
|
|
|
header.realtime.tv_sec = ts->tv_sec;
|
|
|
|
header.realtime.tv_nsec = ts->tv_nsec;
|
|
|
|
|
|
|
|
newVec[0].iov_base = (unsigned char*)&pmsgHeader;
|
|
|
|
newVec[0].iov_len = sizeof(pmsgHeader);
|
|
|
|
newVec[1].iov_base = (unsigned char*)&header;
|
|
|
|
newVec[1].iov_len = sizeof(header);
|
|
|
|
|
|
|
|
for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
|
|
|
|
newVec[i].iov_base = vec[i - headerLength].iov_base;
|
|
|
|
payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
|
|
|
|
|
|
|
|
if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
|
|
|
|
newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
|
|
|
|
if (newVec[i].iov_len) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
payloadSize = LOGGER_ENTRY_MAX_PAYLOAD;
|
|
|
|
break;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
pmsgHeader.len += payloadSize;
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
ret = TEMP_FAILURE_RETRY(writev(atomic_load(&pmsgLoggerWrite.context.fd), newVec, i));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = errno ? -errno : -ENOTCONN;
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) {
|
|
|
|
ret -= sizeof(header) - sizeof(pmsgHeader);
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2016-03-10 18:50:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Virtual pmsg filesystem
|
|
|
|
*
|
|
|
|
* Payload will comprise the string "<basedir>:<basefile>\0<content>" to a
|
|
|
|
* maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the
|
|
|
|
* file.
|
|
|
|
*
|
|
|
|
* Will hijack the header.realtime.tv_nsec field for a sequence number in usec.
|
|
|
|
*/
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static inline const char* strnrchr(const char* buf, size_t len, char c) {
|
|
|
|
const char* cp = buf + len;
|
|
|
|
while ((--cp > buf) && (*cp != c))
|
|
|
|
;
|
|
|
|
if (cp <= buf) {
|
|
|
|
return buf + len;
|
|
|
|
}
|
|
|
|
return cp;
|
2016-03-10 18:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a buffer as filename references (tag = <basedir>:<basename>) */
|
2019-02-08 20:46:19 +01:00
|
|
|
ssize_t __android_log_pmsg_file_write(log_id_t logId, char prio, const char* filename,
|
|
|
|
const char* buf, size_t len) {
|
2017-03-09 17:09:43 +01:00
|
|
|
bool weOpened;
|
|
|
|
size_t length, packet_len;
|
|
|
|
const char* tag;
|
|
|
|
char *cp, *slash;
|
|
|
|
struct timespec ts;
|
|
|
|
struct iovec vec[3];
|
|
|
|
|
|
|
|
/* Make sure the logId value is not a bad idea */
|
|
|
|
if ((logId == LOG_ID_KERNEL) || /* Verbotten */
|
|
|
|
(logId == LOG_ID_EVENTS) || /* Do not support binary content */
|
|
|
|
(logId == LOG_ID_SECURITY) || /* Bad idea to allow */
|
|
|
|
((unsigned)logId >= 32)) { /* fit within logMask on arch32 */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_gettime(android_log_clockid(), &ts);
|
|
|
|
|
|
|
|
cp = strdup(filename);
|
|
|
|
if (!cp) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = cp;
|
|
|
|
slash = strrchr(cp, '/');
|
|
|
|
if (slash) {
|
|
|
|
*slash = ':';
|
|
|
|
slash = strrchr(cp, '/');
|
|
|
|
if (slash) {
|
|
|
|
tag = slash + 1;
|
2016-03-10 18:50:08 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-10 18:50:08 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
length = strlen(tag) + 1;
|
|
|
|
packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length;
|
2016-03-10 18:50:08 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
vec[0].iov_base = &prio;
|
|
|
|
vec[0].iov_len = sizeof(char);
|
|
|
|
vec[1].iov_base = (unsigned char*)tag;
|
|
|
|
vec[1].iov_len = length;
|
|
|
|
|
|
|
|
weOpened = false;
|
2019-01-10 19:37:36 +01:00
|
|
|
for (ts.tv_nsec = 0, length = len; length; ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
|
2017-03-09 17:09:43 +01:00
|
|
|
ssize_t ret;
|
|
|
|
size_t transfer;
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
|
2017-03-09 17:09:43 +01:00
|
|
|
len -= length;
|
|
|
|
break;
|
2016-03-10 18:50:08 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
transfer = length;
|
|
|
|
if (transfer > packet_len) {
|
|
|
|
transfer = strnrchr(buf, packet_len - 1, '\n') - buf;
|
|
|
|
if ((transfer < length) && (buf[transfer] == '\n')) {
|
|
|
|
++transfer;
|
|
|
|
}
|
2016-03-10 18:50:08 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
vec[2].iov_base = (unsigned char*)buf;
|
|
|
|
vec[2].iov_len = transfer;
|
|
|
|
|
|
|
|
if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
|
|
|
|
if (!weOpened) { /* Impossible for weOpened = true here */
|
|
|
|
__android_log_lock();
|
|
|
|
}
|
|
|
|
weOpened = atomic_load(&pmsgLoggerWrite.context.fd) < 0;
|
|
|
|
if (!weOpened) {
|
|
|
|
__android_log_unlock();
|
|
|
|
} else if (pmsgOpen() < 0) {
|
|
|
|
__android_log_unlock();
|
|
|
|
free(cp);
|
|
|
|
return -EBADF;
|
|
|
|
}
|
2016-03-10 18:50:08 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
|
|
|
|
ret = pmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0]));
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
if (weOpened) {
|
2016-10-10 16:27:42 +02:00
|
|
|
pmsgClose();
|
|
|
|
__android_log_unlock();
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
free(cp);
|
|
|
|
return ret ? ret : (len - length);
|
2016-10-10 16:27:42 +02:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
length -= transfer;
|
|
|
|
buf += transfer;
|
|
|
|
}
|
|
|
|
if (weOpened) {
|
|
|
|
pmsgClose();
|
|
|
|
__android_log_unlock();
|
|
|
|
}
|
|
|
|
free(cp);
|
|
|
|
return len;
|
2016-03-10 18:50:08 +01:00
|
|
|
}
|