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.
|
|
|
|
*/
|
|
|
|
|
2016-03-14 22:15:50 +01:00
|
|
|
#include <ctype.h>
|
2016-03-01 22:45:42 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-03-14 22:15:50 +01:00
|
|
|
#include <stdlib.h>
|
2016-03-01 22:45:42 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <private/android_logger.h>
|
|
|
|
|
|
|
|
#include "logger.h"
|
|
|
|
|
2019-11-14 19:39:40 +01:00
|
|
|
static int PmsgAvailable(log_id_t logId);
|
|
|
|
static int PmsgVersion(struct logger* logger, struct android_log_transport_context* transp);
|
|
|
|
static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
|
|
|
|
struct log_msg* log_msg);
|
|
|
|
static void PmsgClose(struct logger_list* logger_list,
|
2017-03-09 17:09:43 +01:00
|
|
|
struct android_log_transport_context* transp);
|
2019-11-14 19:39:40 +01:00
|
|
|
static int PmsgClear(struct logger* logger, struct android_log_transport_context* transp);
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
struct android_log_transport_read pmsgLoggerRead = {
|
2019-01-10 19:37:36 +01:00
|
|
|
.name = "pmsg",
|
2019-11-14 19:39:40 +01:00
|
|
|
.available = PmsgAvailable,
|
|
|
|
.version = PmsgVersion,
|
|
|
|
.close = PmsgClose,
|
|
|
|
.read = PmsgRead,
|
2019-01-10 19:37:36 +01:00
|
|
|
.poll = NULL,
|
2019-11-14 19:39:40 +01:00
|
|
|
.clear = PmsgClear,
|
2019-01-10 19:37:36 +01:00
|
|
|
.setSize = NULL,
|
|
|
|
.getSize = NULL,
|
|
|
|
.getReadableSize = NULL,
|
|
|
|
.getPrune = NULL,
|
|
|
|
.setPrune = NULL,
|
|
|
|
.getStats = NULL,
|
2016-03-01 22:45:42 +01:00
|
|
|
};
|
|
|
|
|
2019-11-14 19:39:40 +01:00
|
|
|
static int PmsgAvailable(log_id_t logId) {
|
2017-03-09 17:09:43 +01:00
|
|
|
if (logId > LOG_ID_SECURITY) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (access("/dev/pmsg0", W_OK) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EBADF;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 19:39:40 +01:00
|
|
|
static int PmsgClear(struct logger*, struct android_log_transport_context*) {
|
2019-10-16 01:06:06 +02:00
|
|
|
return unlink("/sys/fs/pstore/pmsg-ramoops-0");
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* returns the logger version
|
|
|
|
*/
|
2019-11-14 19:39:40 +01:00
|
|
|
static int PmsgVersion(struct logger*, struct android_log_transport_context*) {
|
2017-03-09 17:09:43 +01:00
|
|
|
return 4;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 19:39:40 +01:00
|
|
|
static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
|
|
|
|
struct log_msg* log_msg) {
|
2017-03-09 17:09:43 +01:00
|
|
|
ssize_t ret;
|
|
|
|
off_t current, next;
|
|
|
|
struct __attribute__((__packed__)) {
|
|
|
|
android_pmsg_log_header_t p;
|
|
|
|
android_log_header_t l;
|
|
|
|
uint8_t prio;
|
|
|
|
} buf;
|
|
|
|
static uint8_t preread_count;
|
|
|
|
|
|
|
|
memset(log_msg, 0, sizeof(*log_msg));
|
|
|
|
|
|
|
|
if (atomic_load(&transp->context.fd) <= 0) {
|
|
|
|
int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
return -errno;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (fd == 0) { /* Argggg */
|
|
|
|
fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
|
|
|
|
close(0);
|
|
|
|
if (fd < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = atomic_exchange(&transp->context.fd, fd);
|
|
|
|
if ((i > 0) && (i != fd)) {
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
preread_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (preread_count < sizeof(buf)) {
|
|
|
|
fd = atomic_load(&transp->context.fd);
|
|
|
|
if (fd <= 0) {
|
|
|
|
return -EBADF;
|
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
ret = TEMP_FAILURE_RETRY(read(fd, &buf.p.magic + preread_count, sizeof(buf) - preread_count));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
preread_count += ret;
|
|
|
|
}
|
|
|
|
if (preread_count != sizeof(buf)) {
|
|
|
|
return preread_count ? -EIO : -EAGAIN;
|
|
|
|
}
|
|
|
|
if ((buf.p.magic != LOGGER_MAGIC) || (buf.p.len <= sizeof(buf)) ||
|
2019-01-10 19:37:36 +01:00
|
|
|
(buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD)) || (buf.l.id >= LOG_ID_MAX) ||
|
|
|
|
(buf.l.realtime.tv_nsec >= NS_PER_SEC) ||
|
2017-03-09 17:09:43 +01:00
|
|
|
((buf.l.id != LOG_ID_EVENTS) && (buf.l.id != LOG_ID_SECURITY) &&
|
2019-01-10 19:37:36 +01:00
|
|
|
((buf.prio == ANDROID_LOG_UNKNOWN) || (buf.prio == ANDROID_LOG_DEFAULT) ||
|
2017-03-09 17:09:43 +01:00
|
|
|
(buf.prio >= ANDROID_LOG_SILENT)))) {
|
|
|
|
do {
|
|
|
|
memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
|
|
|
|
} while (preread_count && (buf.p.magic != LOGGER_MAGIC));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
preread_count = 0;
|
|
|
|
|
|
|
|
if ((transp->logMask & (1 << buf.l.id)) &&
|
|
|
|
((!logger_list->start.tv_sec && !logger_list->start.tv_nsec) ||
|
|
|
|
((logger_list->start.tv_sec <= buf.l.realtime.tv_sec) &&
|
|
|
|
((logger_list->start.tv_sec != buf.l.realtime.tv_sec) ||
|
|
|
|
(logger_list->start.tv_nsec <= buf.l.realtime.tv_nsec)))) &&
|
|
|
|
(!logger_list->pid || (logger_list->pid == buf.p.pid))) {
|
2019-10-16 01:53:11 +02:00
|
|
|
char* msg = log_msg->entry.msg;
|
2019-10-16 01:06:06 +02:00
|
|
|
*msg = buf.prio;
|
|
|
|
fd = atomic_load(&transp->context.fd);
|
|
|
|
if (fd <= 0) {
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
ret = TEMP_FAILURE_RETRY(read(fd, msg + sizeof(buf.prio), buf.p.len - sizeof(buf)));
|
|
|
|
if (ret < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
|
2019-10-16 01:53:11 +02:00
|
|
|
log_msg->entry.len = buf.p.len - sizeof(buf) + sizeof(buf.prio);
|
|
|
|
log_msg->entry.hdr_size = sizeof(log_msg->entry);
|
|
|
|
log_msg->entry.pid = buf.p.pid;
|
|
|
|
log_msg->entry.tid = buf.l.tid;
|
|
|
|
log_msg->entry.sec = buf.l.realtime.tv_sec;
|
|
|
|
log_msg->entry.nsec = buf.l.realtime.tv_nsec;
|
|
|
|
log_msg->entry.lid = buf.l.id;
|
|
|
|
log_msg->entry.uid = buf.p.uid;
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2019-10-16 01:53:11 +02:00
|
|
|
return ret + sizeof(buf.prio) + log_msg->entry.hdr_size;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
fd = atomic_load(&transp->context.fd);
|
|
|
|
if (fd <= 0) {
|
|
|
|
return -EBADF;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
|
|
|
|
if (current < 0) {
|
|
|
|
return -errno;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
fd = atomic_load(&transp->context.fd);
|
|
|
|
if (fd <= 0) {
|
|
|
|
return -EBADF;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
next = TEMP_FAILURE_RETRY(lseek(fd, (off_t)(buf.p.len - sizeof(buf)), SEEK_CUR));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (next < 0) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2019-11-14 19:39:40 +01:00
|
|
|
static void PmsgClose(struct logger_list*, struct android_log_transport_context* transp) {
|
2017-03-09 17:09:43 +01:00
|
|
|
int fd = atomic_exchange(&transp->context.fd, 0);
|
|
|
|
if (fd > 0) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2018-10-03 23:10:00 +02:00
|
|
|
static void* realloc_or_free(void* ptr, size_t new_size) {
|
|
|
|
void* result = realloc(ptr, new_size);
|
|
|
|
if (!result) {
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-08 20:46:19 +01:00
|
|
|
ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
|
|
|
|
__android_log_pmsg_file_read_fn fn, void* arg) {
|
2017-03-09 17:09:43 +01:00
|
|
|
ssize_t ret;
|
2019-11-14 19:39:40 +01:00
|
|
|
struct logger_list logger_list;
|
2017-03-09 17:09:43 +01:00
|
|
|
struct android_log_transport_context transp;
|
|
|
|
struct content {
|
|
|
|
struct listnode node;
|
2019-10-16 01:53:11 +02:00
|
|
|
struct logger_entry entry;
|
2017-03-09 17:09:43 +01:00
|
|
|
} * content;
|
|
|
|
struct names {
|
|
|
|
struct listnode node;
|
|
|
|
struct listnode content;
|
|
|
|
log_id_t id;
|
|
|
|
char prio;
|
|
|
|
char name[];
|
|
|
|
} * names;
|
|
|
|
struct listnode name_list;
|
|
|
|
struct listnode *node, *n;
|
|
|
|
size_t len, prefix_len;
|
|
|
|
|
|
|
|
if (!fn) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add just enough clues in logger_list and transp to make API function */
|
|
|
|
memset(&logger_list, 0, sizeof(logger_list));
|
|
|
|
memset(&transp, 0, sizeof(transp));
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY;
|
2017-03-09 17:09:43 +01:00
|
|
|
transp.logMask = (unsigned)-1;
|
|
|
|
if (logId != LOG_ID_ANY) {
|
|
|
|
transp.logMask = (1 << logId);
|
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
transp.logMask &= ~((1 << LOG_ID_KERNEL) | (1 << LOG_ID_EVENTS) | (1 << LOG_ID_SECURITY));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!transp.logMask) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize name list */
|
|
|
|
list_init(&name_list);
|
|
|
|
|
|
|
|
ret = SSIZE_MAX;
|
|
|
|
|
|
|
|
/* Validate incoming prefix, shift until it contains only 0 or 1 : or / */
|
|
|
|
prefix_len = 0;
|
|
|
|
if (prefix) {
|
|
|
|
const char *prev = NULL, *last = NULL, *cp = prefix;
|
|
|
|
while ((cp = strpbrk(cp, "/:"))) {
|
|
|
|
prev = last;
|
|
|
|
last = cp;
|
|
|
|
cp = cp + 1;
|
|
|
|
}
|
|
|
|
if (prev) {
|
|
|
|
prefix = prev + 1;
|
|
|
|
}
|
|
|
|
prefix_len = strlen(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file content */
|
2019-10-16 19:16:44 +02:00
|
|
|
log_msg log_msg;
|
2019-11-14 19:39:40 +01:00
|
|
|
while (PmsgRead(&logger_list, &transp, &log_msg) > 0) {
|
2019-01-10 19:37:36 +01:00
|
|
|
const char* cp;
|
2019-10-16 19:16:44 +02:00
|
|
|
size_t hdr_size = log_msg.entry.hdr_size;
|
2019-10-16 01:53:11 +02:00
|
|
|
|
2019-10-16 19:16:44 +02:00
|
|
|
char* msg = (char*)&log_msg + hdr_size;
|
2019-01-10 19:37:36 +01:00
|
|
|
const char* split = NULL;
|
2017-03-09 17:09:43 +01:00
|
|
|
|
2019-10-16 19:16:44 +02:00
|
|
|
if (hdr_size != sizeof(log_msg.entry)) {
|
2017-03-09 17:09:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Check for invalid sequence number */
|
2019-10-16 19:16:44 +02:00
|
|
|
if (log_msg.entry.nsec % ANDROID_LOG_PMSG_FILE_SEQUENCE ||
|
|
|
|
(log_msg.entry.nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >=
|
|
|
|
ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
|
2017-03-09 17:09:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* Determine if it has <dirbase>:<filebase> format for tag */
|
2019-10-16 19:16:44 +02:00
|
|
|
len = log_msg.entry.len - sizeof(prio);
|
2019-01-10 19:37:36 +01:00
|
|
|
for (cp = msg + sizeof(prio); *cp && isprint(*cp) && !isspace(*cp) && --len; ++cp) {
|
2017-03-09 17:09:43 +01:00
|
|
|
if (*cp == ':') {
|
|
|
|
if (split) {
|
|
|
|
break;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
split = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*cp || !split) {
|
|
|
|
continue;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* Filters */
|
|
|
|
if (prefix_len && strncmp(msg + sizeof(prio), prefix, prefix_len)) {
|
|
|
|
size_t offset;
|
|
|
|
/*
|
|
|
|
* Allow : to be a synonym for /
|
|
|
|
* Things we do dealing with const char * and do not alloc
|
|
|
|
*/
|
|
|
|
split = strchr(prefix, ':');
|
|
|
|
if (split) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
split = strchr(prefix, '/');
|
|
|
|
if (!split) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
offset = split - prefix;
|
2019-01-10 19:37:36 +01:00
|
|
|
if ((msg[offset + sizeof(prio)] != ':') || strncmp(msg + sizeof(prio), prefix, offset)) {
|
2017-03-09 17:09:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++offset;
|
2019-01-10 19:37:36 +01:00
|
|
|
if ((prefix_len > offset) &&
|
|
|
|
strncmp(&msg[offset + sizeof(prio)], split + 1, prefix_len - offset)) {
|
2017-03-09 17:09:43 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if ((prio != ANDROID_LOG_ANY) && (*msg < prio)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* check if there is an existing entry */
|
|
|
|
list_for_each(node, &name_list) {
|
|
|
|
names = node_to_item(node, struct names, node);
|
2019-10-16 19:16:44 +02:00
|
|
|
if (!strcmp(names->name, msg + sizeof(prio)) && names->id == log_msg.entry.lid &&
|
|
|
|
names->prio == *msg) {
|
2017-03-09 17:09:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* We do not have an existing entry, create and add one */
|
|
|
|
if (node == &name_list) {
|
|
|
|
static const char numbers[] = "0123456789";
|
|
|
|
unsigned long long nl;
|
|
|
|
|
|
|
|
len = strlen(msg + sizeof(prio)) + 1;
|
2019-01-10 19:37:36 +01:00
|
|
|
names = static_cast<struct names*>(calloc(1, sizeof(*names) + len));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!names) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
strcpy(names->name, msg + sizeof(prio));
|
2019-10-16 19:16:44 +02:00
|
|
|
names->id = static_cast<log_id_t>(log_msg.entry.lid);
|
2017-03-09 17:09:43 +01:00
|
|
|
names->prio = *msg;
|
|
|
|
list_init(&names->content);
|
|
|
|
/*
|
|
|
|
* Insert in reverse numeric _then_ alpha sorted order as
|
|
|
|
* representative of log rotation:
|
|
|
|
*
|
|
|
|
* log.10
|
|
|
|
* klog.10
|
|
|
|
* . . .
|
|
|
|
* log.2
|
|
|
|
* klog.2
|
|
|
|
* log.1
|
|
|
|
* klog.1
|
|
|
|
* log
|
|
|
|
* klog
|
|
|
|
*
|
|
|
|
* thus when we present the content, we are provided the oldest
|
|
|
|
* first, which when 'refreshed' could spill off the end of the
|
|
|
|
* pmsg FIFO but retaining the newest data for last with best
|
|
|
|
* chances to survive.
|
|
|
|
*/
|
|
|
|
nl = 0;
|
|
|
|
cp = strpbrk(names->name, numbers);
|
|
|
|
if (cp) {
|
|
|
|
nl = strtoull(cp, NULL, 10);
|
|
|
|
}
|
|
|
|
list_for_each_reverse(node, &name_list) {
|
|
|
|
struct names* a_name = node_to_item(node, struct names, node);
|
|
|
|
const char* r = a_name->name;
|
|
|
|
int compare = 0;
|
|
|
|
|
|
|
|
unsigned long long nr = 0;
|
|
|
|
cp = strpbrk(r, numbers);
|
|
|
|
if (cp) {
|
|
|
|
nr = strtoull(cp, NULL, 10);
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (nr != nl) {
|
|
|
|
compare = (nl > nr) ? 1 : -1;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (compare == 0) {
|
|
|
|
compare = strcmp(names->name, r);
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (compare <= 0) {
|
|
|
|
break;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
list_add_head(node, &names->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove any file fragments that match our sequence number */
|
|
|
|
list_for_each_safe(node, n, &names->content) {
|
|
|
|
content = node_to_item(node, struct content, node);
|
2019-10-16 19:16:44 +02:00
|
|
|
if (log_msg.entry.nsec == content->entry.nsec) {
|
2017-03-09 17:09:43 +01:00
|
|
|
list_remove(&content->node);
|
|
|
|
free(content);
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 22:15:50 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* Add content */
|
2019-01-10 19:37:36 +01:00
|
|
|
content = static_cast<struct content*>(
|
2019-10-16 19:16:44 +02:00
|
|
|
calloc(1, sizeof(content->node) + hdr_size + log_msg.entry.len));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!content) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-16 19:16:44 +02:00
|
|
|
memcpy(&content->entry, &log_msg.entry, hdr_size + log_msg.entry.len);
|
2017-03-09 17:09:43 +01:00
|
|
|
|
|
|
|
/* Insert in sequence number sorted order, to ease reconstruction */
|
|
|
|
list_for_each_reverse(node, &names->content) {
|
2019-10-16 19:16:44 +02:00
|
|
|
if ((node_to_item(node, struct content, node))->entry.nsec < log_msg.entry.nsec) {
|
2017-03-09 17:09:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_add_head(node, &content->node);
|
|
|
|
}
|
2019-11-14 19:39:40 +01:00
|
|
|
PmsgClose(&logger_list, &transp);
|
2017-03-09 17:09:43 +01:00
|
|
|
|
|
|
|
/* Progress through all the collected files */
|
|
|
|
list_for_each_safe(node, n, &name_list) {
|
|
|
|
struct listnode *content_node, *m;
|
|
|
|
char* buf;
|
|
|
|
size_t sequence, tag_len;
|
|
|
|
|
|
|
|
names = node_to_item(node, struct names, node);
|
|
|
|
|
|
|
|
/* Construct content into a linear buffer */
|
|
|
|
buf = NULL;
|
|
|
|
len = 0;
|
|
|
|
sequence = 0;
|
|
|
|
tag_len = strlen(names->name) + sizeof(char); /* tag + nul */
|
|
|
|
list_for_each_safe(content_node, m, &names->content) {
|
|
|
|
ssize_t add_len;
|
|
|
|
|
|
|
|
content = node_to_item(content_node, struct content, node);
|
|
|
|
add_len = content->entry.len - tag_len - sizeof(prio);
|
|
|
|
if (add_len <= 0) {
|
|
|
|
list_remove(content_node);
|
|
|
|
free(content);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buf) {
|
2019-01-10 19:37:36 +01:00
|
|
|
buf = static_cast<char*>(malloc(sizeof(char)));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
list_remove(content_node);
|
|
|
|
free(content);
|
|
|
|
continue;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Missing sequence numbers */
|
|
|
|
while (sequence < content->entry.nsec) {
|
|
|
|
/* plus space for enforced nul */
|
2019-01-10 19:37:36 +01:00
|
|
|
buf = static_cast<char*>(realloc_or_free(buf, len + sizeof(char) + sizeof(char)));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!buf) {
|
|
|
|
break;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
buf[len] = '\f'; /* Mark missing content with a form feed */
|
|
|
|
buf[++len] = '\0';
|
|
|
|
sequence += ANDROID_LOG_PMSG_FILE_SEQUENCE;
|
|
|
|
}
|
|
|
|
if (!buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
list_remove(content_node);
|
|
|
|
free(content);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* plus space for enforced nul */
|
2019-01-10 19:37:36 +01:00
|
|
|
buf = static_cast<char*>(realloc_or_free(buf, len + add_len + sizeof(char)));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (!buf) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
list_remove(content_node);
|
|
|
|
free(content);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
memcpy(buf + len, (char*)&content->entry + content->entry.hdr_size + tag_len + sizeof(prio),
|
2017-03-09 17:09:43 +01:00
|
|
|
add_len);
|
|
|
|
len += add_len;
|
|
|
|
buf[len] = '\0'; /* enforce trailing hidden nul */
|
|
|
|
sequence = content->entry.nsec + ANDROID_LOG_PMSG_FILE_SEQUENCE;
|
|
|
|
|
|
|
|
list_remove(content_node);
|
|
|
|
free(content);
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
if (buf) {
|
|
|
|
if (len) {
|
|
|
|
/* Buffer contains enforced trailing nul just beyond length */
|
|
|
|
ssize_t r;
|
|
|
|
*strchr(names->name, ':') = '/'; /* Convert back to filename */
|
|
|
|
r = (*fn)(names->id, names->prio, names->name, buf, len, arg);
|
|
|
|
if ((ret >= 0) && (r > 0)) {
|
|
|
|
if (ret == SSIZE_MAX) {
|
|
|
|
ret = r;
|
|
|
|
} else {
|
|
|
|
ret += r;
|
|
|
|
}
|
|
|
|
} else if (r < ret) {
|
|
|
|
ret = r;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
free(buf);
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
list_remove(node);
|
|
|
|
free(names);
|
|
|
|
}
|
|
|
|
return (ret == SSIZE_MAX) ? -ENOENT : ret;
|
2016-03-14 22:15:50 +01:00
|
|
|
}
|