From 026ddde6e41dfa05f10348cb913fae6b2a9b73d7 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Mon, 18 Nov 2019 15:13:47 -0800 Subject: [PATCH] liblog: remove the rest of the log reader transport This is simplified down to the point there are only two branches that need to be made, so remove the rest of the transport structs and simply branch where needed. Test: liblog-unit-tests Change-Id: Ic82e7e70eb7b4e40b381a4d8066629c5b7d4f827 --- liblog/logd_reader.cpp | 52 ++++++++---------------------------------- liblog/logd_reader.h | 4 ++++ liblog/logger.h | 26 +-------------------- liblog/logger_read.cpp | 33 ++++++++++++++------------- liblog/pmsg_reader.cpp | 50 +++++++++++----------------------------- liblog/pmsg_reader.h | 29 +++++++++++++++++++++++ 6 files changed, 73 insertions(+), 121 deletions(-) create mode 100644 liblog/pmsg_reader.h diff --git a/liblog/logd_reader.cpp b/liblog/logd_reader.cpp index 4e2dc6665..c8832aa96 100644 --- a/liblog/logd_reader.cpp +++ b/liblog/logd_reader.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "logd_reader.h" + #include #include #include @@ -35,39 +37,8 @@ #include #include -#include "log_portability.h" -#include "logd_reader.h" #include "logger.h" -static int LogdAvailable(log_id_t LogId); -static int LogdRead(struct logger_list* logger_list, struct android_log_transport_context* transp, - struct log_msg* log_msg); -static void LogdClose(struct logger_list* logger_list, - struct android_log_transport_context* transp); - -struct android_log_transport_read logdLoggerRead = { - .name = "logd", - .available = LogdAvailable, - .close = LogdClose, - .read = LogdRead, -}; - -static int LogdAvailable(log_id_t logId) { - if (logId >= LOG_ID_MAX) { - return -EINVAL; - } - if (logId == LOG_ID_SECURITY) { - uid_t uid = __android_log_uid(); - if (uid != AID_SYSTEM) { - return -EPERM; - } - } - if (access("/dev/socket/logdw", W_OK) == 0) { - return 0; - } - return -EBADF; -} - // Connects to /dev/socket/ and returns the associated fd or returns -1 on error. // O_CLOEXEC is always set. static int socket_local_client(const std::string& name, int type) { @@ -296,15 +267,11 @@ int android_logger_set_prune_list(struct logger_list* logger_list, char* buf, si return check_log_success(buf, SendLogdControlMessage(buf, len)); } -static int logdOpen(struct logger_list* logger_list, struct android_log_transport_context* transp) { +static int logdOpen(struct logger_list* logger_list) { char buffer[256], *cp, c; int ret, remaining, sock; - if (!logger_list) { - return -EINVAL; - } - - sock = atomic_load(&transp->context.sock); + sock = atomic_load(&logger_list->fd); if (sock > 0) { return sock; } @@ -377,7 +344,7 @@ static int logdOpen(struct logger_list* logger_list, struct android_log_transpor return ret; } - ret = atomic_exchange(&transp->context.sock, sock); + ret = atomic_exchange(&logger_list->fd, sock); if ((ret > 0) && (ret != sock)) { close(ret); } @@ -385,9 +352,8 @@ static int logdOpen(struct logger_list* logger_list, struct android_log_transpor } /* Read from the selected logs */ -static int LogdRead(struct logger_list* logger_list, struct android_log_transport_context* transp, - struct log_msg* log_msg) { - int ret = logdOpen(logger_list, transp); +int LogdRead(struct logger_list* logger_list, struct log_msg* log_msg) { + int ret = logdOpen(logger_list); if (ret < 0) { return ret; } @@ -407,8 +373,8 @@ static int LogdRead(struct logger_list* logger_list, struct android_log_transpor } /* Close all the logs */ -static void LogdClose(struct logger_list*, struct android_log_transport_context* transp) { - int sock = atomic_exchange(&transp->context.sock, -1); +void LogdClose(struct logger_list* logger_list) { + int sock = atomic_exchange(&logger_list->fd, -1); if (sock > 0) { close(sock); } diff --git a/liblog/logd_reader.h b/liblog/logd_reader.h index 09f862760..2d032fa22 100644 --- a/liblog/logd_reader.h +++ b/liblog/logd_reader.h @@ -18,10 +18,14 @@ #include +#include "log/log_read.h" #include "log_portability.h" __BEGIN_DECLS +int LogdRead(struct logger_list* logger_list, struct log_msg* log_msg); +void LogdClose(struct logger_list* logger_list); + ssize_t SendLogdControlMessage(char* buf, size_t buf_size); __END_DECLS diff --git a/liblog/logger.h b/liblog/logger.h index d2251e578..d648ff8e5 100644 --- a/liblog/logger.h +++ b/liblog/logger.h @@ -46,32 +46,8 @@ struct android_log_transport_write { size_t nr); }; -struct android_log_transport_context; - -struct android_log_transport_read { - const char* name; /* human name to describe the transport */ - - /* Does not cause resources to be taken */ - int (*available)(log_id_t logId); - /* Release resources taken by the following interfaces */ - void (*close)(struct logger_list* logger_list, struct android_log_transport_context* transp); - /* - * Expect all to instantiate open automagically on any call, - * so we do not have an explicit open call. - */ - int (*read)(struct logger_list* logger_list, struct android_log_transport_context* transp, - struct log_msg* log_msg); -}; - -struct android_log_transport_context { - union android_log_context_union context; /* zero init per-transport context */ - - struct android_log_transport_read* transport; -}; - struct logger_list { - android_log_transport_context transport_context; - bool transport_initialized; + atomic_int fd; int mode; unsigned int tail; log_time start; diff --git a/liblog/logger_read.cpp b/liblog/logger_read.cpp index 0ce7a465d..c65501c48 100644 --- a/liblog/logger_read.cpp +++ b/liblog/logger_read.cpp @@ -31,7 +31,9 @@ #include #include "log_portability.h" +#include "logd_reader.h" #include "logger.h" +#include "pmsg_reader.h" /* method for getting the associated sublog id */ log_id_t android_logger_get_id(struct logger* logger) { @@ -50,14 +52,6 @@ static struct logger_list* android_logger_list_alloc_internal(int mode, unsigned logger_list->tail = tail; logger_list->pid = pid; -#if (FAKE_LOG_DEVICE == 0) - extern struct android_log_transport_read logdLoggerRead; - extern struct android_log_transport_read pmsgLoggerRead; - - logger_list->transport_context.transport = - (mode & ANDROID_LOG_PSTORE) ? &pmsgLoggerRead : &logdLoggerRead; -#endif - return logger_list; } @@ -100,14 +94,19 @@ struct logger_list* android_logger_list_open(log_id_t logId, int mode, unsigned } int android_logger_list_read(struct logger_list* logger_list, struct log_msg* log_msg) { - if (logger_list == nullptr || logger_list->transport_context.transport == nullptr || - logger_list->log_mask == 0) { + if (logger_list == nullptr || logger_list->log_mask == 0) { return -EINVAL; } - android_log_transport_context* transp = &logger_list->transport_context; + int ret = 0; - int ret = (*transp->transport->read)(logger_list, transp, log_msg); +#if (FAKE_LOG_DEVICE == 0) + if (logger_list->mode & ANDROID_LOG_PSTORE) { + ret = PmsgRead(logger_list, log_msg); + } else { + ret = LogdRead(logger_list, log_msg); + } +#endif if (ret <= 0) { return ret; @@ -138,11 +137,13 @@ void android_logger_list_free(struct logger_list* logger_list) { return; } - android_log_transport_context* transport_context = &logger_list->transport_context; - - if (transport_context->transport && transport_context->transport->close) { - (*transport_context->transport->close)(logger_list, transport_context); +#if (FAKE_LOG_DEVICE == 0) + if (logger_list->mode & ANDROID_LOG_PSTORE) { + PmsgClose(logger_list); + } else { + LogdClose(logger_list); } +#endif free(logger_list); } diff --git a/liblog/pmsg_reader.cpp b/liblog/pmsg_reader.cpp index 9f603e959..9390fecae 100644 --- a/liblog/pmsg_reader.cpp +++ b/liblog/pmsg_reader.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "pmsg_reader.h" + #include #include #include @@ -26,31 +28,7 @@ #include "logger.h" -static int PmsgAvailable(log_id_t logId); -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, - struct android_log_transport_context* transp); - -struct android_log_transport_read pmsgLoggerRead = { - .name = "pmsg", - .available = PmsgAvailable, - .close = PmsgClose, - .read = PmsgRead, -}; - -static int PmsgAvailable(log_id_t logId) { - if (logId > LOG_ID_SECURITY) { - return -EINVAL; - } - if (access("/dev/pmsg0", W_OK) == 0) { - return 0; - } - return -EBADF; -} - -static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp, - struct log_msg* log_msg) { +int PmsgRead(struct logger_list* logger_list, struct log_msg* log_msg) { ssize_t ret; off_t current, next; struct __attribute__((__packed__)) { @@ -62,7 +40,7 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor memset(log_msg, 0, sizeof(*log_msg)); - if (atomic_load(&transp->context.fd) <= 0) { + if (atomic_load(&logger_list->fd) <= 0) { int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC); if (fd < 0) { @@ -75,7 +53,7 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor return -errno; } } - i = atomic_exchange(&transp->context.fd, fd); + i = atomic_exchange(&logger_list->fd, fd); if ((i > 0) && (i != fd)) { close(i); } @@ -86,7 +64,7 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor int fd; if (preread_count < sizeof(buf)) { - fd = atomic_load(&transp->context.fd); + fd = atomic_load(&logger_list->fd); if (fd <= 0) { return -EBADF; } @@ -120,7 +98,7 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor (!logger_list->pid || (logger_list->pid == buf.p.pid))) { char* msg = log_msg->entry.msg; *msg = buf.prio; - fd = atomic_load(&transp->context.fd); + fd = atomic_load(&logger_list->fd); if (fd <= 0) { return -EBADF; } @@ -144,7 +122,7 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor return ret + sizeof(buf.prio) + log_msg->entry.hdr_size; } - fd = atomic_load(&transp->context.fd); + fd = atomic_load(&logger_list->fd); if (fd <= 0) { return -EBADF; } @@ -152,7 +130,7 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor if (current < 0) { return -errno; } - fd = atomic_load(&transp->context.fd); + fd = atomic_load(&logger_list->fd); if (fd <= 0) { return -EBADF; } @@ -166,8 +144,8 @@ static int PmsgRead(struct logger_list* logger_list, struct android_log_transpor } } -static void PmsgClose(struct logger_list*, struct android_log_transport_context* transp) { - int fd = atomic_exchange(&transp->context.fd, 0); +void PmsgClose(struct logger_list* logger_list) { + int fd = atomic_exchange(&logger_list->fd, 0); if (fd > 0) { close(fd); } @@ -185,7 +163,6 @@ ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* pref __android_log_pmsg_file_read_fn fn, void* arg) { ssize_t ret; struct logger_list logger_list; - struct android_log_transport_context transp; struct content { struct listnode node; struct logger_entry entry; @@ -207,7 +184,6 @@ ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* pref /* 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)); logger_list.mode = ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK | ANDROID_LOG_RDONLY; logger_list.log_mask = (unsigned)-1; @@ -241,7 +217,7 @@ ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* pref /* Read the file content */ log_msg log_msg; - while (PmsgRead(&logger_list, &transp, &log_msg) > 0) { + while (PmsgRead(&logger_list, &log_msg) > 0) { const char* cp; size_t hdr_size = log_msg.entry.hdr_size; @@ -399,7 +375,7 @@ ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* pref } list_add_head(node, &content->node); } - PmsgClose(&logger_list, &transp); + PmsgClose(&logger_list); /* Progress through all the collected files */ list_for_each_safe(node, n, &name_list) { diff --git a/liblog/pmsg_reader.h b/liblog/pmsg_reader.h new file mode 100644 index 000000000..53746d83f --- /dev/null +++ b/liblog/pmsg_reader.h @@ -0,0 +1,29 @@ +/* + * Copyright 2019 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. + */ + +#pragma once + +#include + +#include "log/log_read.h" +#include "log_portability.h" + +__BEGIN_DECLS + +int PmsgRead(struct logger_list* logger_list, struct log_msg* log_msg); +void PmsgClose(struct logger_list* logger_list); + +__END_DECLS